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
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.
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?)
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
精彩评论