3

Most MVC tutorials I've been reading seem to create 4 View objects for each Model. For example, if my Model is "Foo", there seem to be 4 .cshtml files: Foo/Create, Foo/Delete, Foo/Details, and Foo/Edit. Using the VisualStudio "scaffolding" helper does this as well.

Is this really considered MVC best-practice? It just feels wrong to have 4 classes that are 80-90% identical to each other. When I add a new field to Foo, I need to edit all 4 .cshtml files. This sort of dual-maintenance (quad-maintenance?) just makes my OO skin crawl.

Please tell me: is there an expected/accepted best-practice which handles this differently? Or, if this really IS accepted best-practice, tell me why the quad-maintance shouldn't make me squirm.

I'm a reasonably skilled veteran of ASP.NET / c# / OO Design, but pretty new to MVC; so apologies if this is a noob question. Thanks in advance for your help!

Edit: thanks for all the replies! I marked the most thorough one as the answer, but upvoted all that were helpful.

5 Answers 5

5

You'll probably need between two and four different views:

  • List (for viewing many things)
  • View (for viewing a single thing. Might not be necessary, if it's OK to use Edit as View, or if List has room to show all properties)
  • Create
  • Edit (can be the same as create, if you code cleverly)

Thus, if your model doesn't have too many properties to show them all in a table, and if you're OK with not having a static, non-editable view for just examining, you can get well away with just List and Edit, and scrap the other two.

However, this doesn't solve your problem of double (or triple) maintenance if you update your model. There's other magic for that ;)

In ASP.NET MVC 3, there are extensions on HtmlHelper that let you do Html.DisplayForModel() and Html.EditorForModel(). These use predefined templates to nest themselves into your object and draw up display/edit fields for all public properites. If you pass DisplayForModel an IEnumerable<Foo>, it will create a table with column headers that are the property names of Foo (using the DisplayName attribute information if you supplied it) and where each row represent one Foo instance. If you give EditorForModel a Foo, it will create a <label> and an <input> for each public property on Foo.

All of the templates used by these powerful extension methods can be replaced by you, if you're not happy with the defaults. This can be done either on the level of Foo, in which case you'd be back in your double-maintenance scenario, or on lower levels (such as string or DateTime) to affect all editor/display fields generated with the templates.

For more information on exactly how this works, google "ASP.NET MVC 3 editor templates" and read a couple of tutorials. They'll explain the details much better than I could.

1
  • The generated forms use Html.EditorFor(), but EditorForModel() is new to me - I'll definitely give it a look. Overall, it sounds like the generated CRUD-View code is nice for RAD and proof-of-concept. But for long-term maintainable code, there's better ways to implement my CRUD actions. Thanks for your in-depth response!
    – mikemanne
    Oct 12, 2011 at 21:00
1

The views that ASP.NET MVC create for you don't necessarily need to be the views that you use in production. I found those just to be handy while developing quick prototypes or to test the database CRUD operations. Feel free to create whatever view(s) you would like to handle the operations.

I would generally just have 1 or 2 views to handle the basic operations and not use the built in views that are generated. For example, 1 view for adding, editing, or details and 1 view to show a list of objects.

1
  • Thanks Tim! I had my suspicion that the generated 4-separate-view-classes might not be the best real-world approach.
    – mikemanne
    Oct 12, 2011 at 20:50
1

It all depends on your application.

If you have a single item, you don't need a List view. If you can't edit it, then you don't need an edit view. Create and Edit can often be the same view, unless there are special things you need to do in one, but not the other.

In other words, use as many views as you need. There's no hard and fast rule here. The scaffolding is just there to help you on your way. Many kinds of apps will work just fine using the scaffolding, and won't require advanced HTML or Javascript.

Why would you want multiple views? Well, let's take the display and edit functions. You could create one view, in which you use if statements to determine the edit mode of the view, however this will complicated the view logic and views should be as simple as possible.

The reason to create seperate views is that its easier to maintain than one gigantic view with tons of conditional logic in it.

0

You can use exactly the same view when you are performing [HttpGet]. Given that you pass a proper ViewModel to this view, it will populate with appropriate data every time whether you are loading create, update, or delete Action.

The problem becomes apparent when you try to post that data to a specific Action. Naturally View should have only one form, which will be used for posting data. When you declare this form, you specify which exactly Action to use for Post.

Having 3 different Submit buttons in that form will not make a difference since all of them will post the same form to the same Action.

You could do some javascript tweaking on OnClick event for these buttons to change Action to which data is posted, but this definitively would not be best practice.

Buttom line: having 4 different views for each of the CRUD actions is the best practice for MVC.

2
  • My understanding is there are different overloads of Html.BeginForm() which would allow me to dynamically specify the Action to be performed, at the time I'm rendering that form. Thus, I don't think I need 4 different View classes just to generate forms with 4 different actions.
    – mikemanne
    Oct 12, 2011 at 20:41
  • Correct. Now that you mentioned it I gave it a bit more of a thought... You could pass the Action name to the view in the ViewBag and just assign that value to the Action parameter in the declaration of BeginForm. Any overload of BeginForm will allow you to do it. Oct 12, 2011 at 21:36
0

I tend to create the following for an object's CRUD ops:

  • index
  • _form (partial)
  • new
  • update
  • delete
  • view

As the same form is shared between new and update, there is very little difference between the two. It really depends on how much you want the variation to be, honestly.

As for delete, this is optional. I like to have a view in case javascript is disabled.

edit: You mention view models and the guy above posted a long, convoluted (no offense) VM code sample.

Personally, I hate classes written to basically mirror domain objects and are only used to "move" data. I hate VMs. I hate DTOs. I hate everything that makes me have to write more code than is necessary.

I guess I've drank the coolaid of other frameworks (rails, sinatra, node.js) to the point where I can't stomach the idea of tossing DRY to the wind.

I personally say skip um.

Edit2 I forgot list..

5
  • @mikemanne right, so you basically create a form that encompasses what you'll need for the model (including a hidden id) and pass it over the model that you are expecting. Keep in mind that you'll need to create a new instance of your model on the initial new request. Subsequent requests (as well as update requests) should have an instance to operate with.
    – Chance
    Oct 12, 2011 at 20:09
  • So your "new" and "update" both call RenderPartial to include the common form fields defined in _form? That's starting to feel like more familiar territory. :) In the past, I've used the same form for "details" as well, with all fields set to readonly.
    – mikemanne
    Oct 12, 2011 at 20:32
  • 2
    I'd stray away from "details" being in form fashion. It is very webformsie and does not "fit" the web, so to speak. Use appropriate markup (p, span, quote, h1-h6, and so on) so that it can be properly parsed.
    – Chance
    Oct 12, 2011 at 20:37
  • I recently worked on a page that replaced a literal paper form. It was large, with a lot of different fields. In that case, using an actual readonly form as my "view" was beneficial, since the "view" and "edit" pages looked completely identical, and both were similar to the paper form with which the client was familiar. It was also nice to only have to change one class when they constantly added and removed fields! :) However, I understand your point, and agree that there's lots of times when a dedicated div/span/p -based readonly view would be a better approach.
    – mikemanne
    Oct 12, 2011 at 20:49
  • +1 to offset the drive-by-downvote (and because your answered deserved it anyway). I completely disagree with you on the value of ViewModels - but that's a discussion for another day, over a couple BEvERages. ;)
    – mikemanne
    Oct 12, 2011 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.