tags:

views:

62

answers:

3

Working for months now in ASP.NET MVC I am starting to walk around in circles - I am constantly bumping into Edit views that are the same as Create views. Should I continue just ditching the Edit views and try harder to make one View for both Create and Edit? Because essentially they are the same, they do the same validation the only difference is in the controller where I do Update instead of Create. And I can do that with a simple if..else statement - if an object is found in datastore, do the update, else do the create.

What do you think? Is it likely that I ever run into problems in the future if I decide to get rid of Edit views??

Clarification:

The main question is: What kind of changes/business demands could come up that would require separate Create/Edit? This is actually what I would like to find out. Because if we have a Page object, that has, for instance these properties: Title, URL, Active, etc. and require those fields to be entered in at Create, why would we allow them to be empty for whatever reason at Edit?

thanx

+3  A: 

Use a UserControl (Form.ascx for example), and use it in both Create.aspx and Edit.aspx

<% using (var form = Html.BeginForm()) {
    Html.RenderPartial("Form");
<% } %>

This way, the Post is happening en each of Create Edit methods of controller, you can also in both methods call the same Validation method, and the do the respective insert or update.

Alex LE
+1 Great answer. Partial Views make life a lot easier. One thing to note is that the partial should be strongly typed so you can pass the model object in with the edit view.
Scott Anderson
One more thing to the OP. check out the NerdDinner tutorial. On page 111, ScottGu shows exactly how to do what Alex is talking about with partial views. Find the tutorial here: http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf
Scott Anderson
This is actually what I have been doing so far. With Partials but I had View User control splitted - I mean Create.ascx and Edit.ascx and then Create.aspx and Edit.aspx and it really annoying maintaing all that when all object fields are the same. It's just copy pasting and renaming files, which I think is wrong.
mare
A: 

In your custom view model you can pass a parameter e.g: EditMode with the mode you are currently using (Edit or Create) and take a differemnt action anme in function of this mode

Gregoire
+1  A: 

If it's consistently costing you development time, then I'd join them... deal with them branching off when the time comes to it.

Also, you might want to look and see if maybe you can use some code generation to make some of your problems easier if you want to keep them separate. (I'm assuming the problems arise when you need to add or remove a new field from both?)

blesh
Exactly. When I add or remove a field I have to make edits to two files.
mare