views:

2010

answers:

7

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

+7  A: 

Scott Gu will show the way

E Rolnicki
Where is it explained? I browsed 3 or 4 times through the article and couldn't find the answer.
Serge - appTranslator
you don't use 1 single form for both as they are by nature two distinctly different actions. You could rip out the fields of a create form, put them in a partial view, and reference that partial in both a create form and an edit form though.
E Rolnicki
A: 

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

ListenToRick
+2  A: 

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.

Craig Stuntz
If you aren't distinguishing using HTTP verbs then using the same action is perfectly reasonable. There is so much duplication otherwise. You simply have to take the ID as a parameter, non-zero ones get fetched first, etc.
Ben Scheirman
the association of verbs with CRUD here is confusing because no such correlation directly exists (discussions: http://reinout.vanrees.org/research/phd/various-stuff/getputpostdelete and http://www.elharo.com/blog/software-development/web-development/2005/12/08/post-vs-put/)
annakata
A: 

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? ;)

Keith Williams
+1  A: 

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

Perpetualcoder
A: 

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.

Trevor de Koekkoek
A: 

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

Serge - appTranslator