开发者

Separate ASP.NET MVC View object for each Model CRUD operation?

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.


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.


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.


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.


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.


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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜