Whats the best practice approach to creating a form that is used to both create new models and edit existing models?
Are there any tutorials that people can point me in the direction of?
Cheers
Whats the best practice approach to creating a form that is used to both create new models and edit existing models?
Are there any tutorials that people can point me in the direction of?
Cheers
Actually, should I use the same view and controller?
Should I actually have a different view and contoller method as suugested in this post?
http://stackoverflow.com/questions/304451/how-to-cleanly-reuse-edit-new-views-in-asp-net-mvc
Do not use the same controller action. New = HTTP PUT; edit = HTTP POST, so that's two different things. Both actions can and should be on the same controller, though.
I like the idea of using a user control for common features (e.g., editors), and wrapping that in action-specific views for stuff which should only appear on new or edit, but not both.
I put the form itself in a user control - say, Views/Shared/WidgetForm.ascx. I put all form fields in this user control, but NOT the form tags themselves.
The views, say Views/Widgets/New.aspx and Views/Widgets/Edit.aspx, have the form tags in them and all the "surroundings" - instructions for filling in the form, page title, etc etc. Then they include the user control inside the form tags.
The user control simply takes a Widget object, and displays a form based on the results. Putting sensible defaults in new Widget options therefore becomes important, but you're doing that anyway, right? ;)
It could be (should be IMO) one controller but different controller actions. Also make sure you have proper HTTP verbs associated with appropriate action. Follow the tutorial posted by E Rolnicki and you will be on your way!
Happy Coding!!
I have a system that I think works pretty well. In my shared views I have 2 generic forms, Edit.aspx and New.aspx
Then in my specific view folder I have a control named EditItems.ascx
In my edit form I have the form tags and specific buttons for edit and in the new form I have the form tags and specific buttons for new. In each I have Html.RenderPartial("EditItems.ascx")
This way your user control can be strongly typed and yet you are reusing the look and feel of the edit and new pages.
Now in some cases, your new page might have a different layout than the Edit page. In that case just add "Edit.aspx" to your specific view folder.
I find this gives me the best combination of reuse while still allowing full customization should I need it. And as for controller actions, yes they should be separate actions.
If the entity has some kind of internal private key (e.g. an "id" member that is always > 0), you can use /Edit/0 instead of /Create