Alex Rivera | Logout

Using ASP.NET MVC, how to best avoid writing both the Add View and Edit View?

Asked 2008-08-20T20:34:12.540
14

The Add view and the Edit view are often incredibly similar that it is unwarranted to write 2 views. As the app evolves you would be making the same changes to both.

However, there are usually subtle differences. For instance, a field might be read-only once it's been added, and if that field is a DropDownList you no longer need that List in the ViewData.

So, should I create a view data class which contains all the information for both views, where, depending on the operation you're performing, certain properties will be null?
Should I include the operation in the view data as an enum?
Should I surround all the subtle differences with <% if( ViewData.Model.Op == Ops.Editing ) { %> ?

Or is there a better way?

Edit
Report

1 Answer

4

I don't like the Views to become too complex, and so far I have tended to have separate views for Edit and Add. I use a user control to store the common elements to avoid repetition. Both of the views will be centered around the same ViewData, and I have a marker on my data to say whether the object is new or an existing object.

This isn't any more elegant than what you have stipulated, so I wonder if any of the Django or Rails guys can provide any input.

I love asp.net mvc but it is still maturing, and still needs more sugar adding to take away some of the friction of creating websites.

answered 2008-08-20T21:18:52.340

Your Answer