Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Razor - Create / Edit view best practice

Tags:

I'm working with MVC 3 / Razor for the first time and it seems odd that all the examples and VS scaffolds for create and edit views all have separate HTML views for these concepts.

There is really not much difference between many Create/Edit forms so I was wondering why I can't find examples of people using a single Update form that can be used by both Create and Edit actions.

I have gotten an Update.cshtml view working but was wondering about how it talks to the Edit or Create action method on the controller.

My questions are:

  1. Anyone have a quick answer to talking to the controller, or
  2. Anyone know of a tutorial showing good practice for working this way, or
  3. Is there some good reason for keeping the Create/Edit views separate when the HTML is often the same.

Cheers Dave

like image 923
David Cruwys Avatar asked Jun 10 '11 10:06

David Cruwys


People also ask

How do you use same view for add and edit in MVC?

Use the same View Model aka CreateUpdateViewModel and discarding extra UpdateBindingModel properties in the view but still posting the corresponding model on POST. Having your binding models as properties and select one or the other in the specific view. (better use @Html.

Is Razor better than MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

How do you add a view on Razor?

Right click the Views\HelloWorld folder and click Add, then click MVC 5 View Page with Layout (Razor). In the Specify Name for Item dialog box, enter Index, and then click OK. In the Select a Layout Page dialog, accept the default _Layout. cshtml and click OK.

When should partial views be used?

Partial views are an effective way to: Break up large markup files into smaller components. In a large, complex markup file composed of several logical pieces, there's an advantage to working with each piece isolated into a partial view.


2 Answers

This (kind!) of question is asked before: ASP.NET MVC - using the same form to both create and edit

Basically you can create a partial view and include it on your Create and Edit view.

Scott Guthrie has a nice post about Partial Views.

(I've read about this somewhere, but can't find it, I'll update this post when I do find it)

like image 138
Rhapsody Avatar answered Oct 13 '22 14:10

Rhapsody


Be mindful that answers to your question should also be driven by business need (and roles). The scaffolding does provide separate functionality, which in some cases is the preferred implementation.

CREATE and EDIT functionality is often pretty much identical from a technical (programming) perspective. This can lead a technical person to think that the functionality should be combined in order to implement a more efficient technical solution. However, any technical implementation must be in response to business need, which might require separation (e.g. by business role) of these concerns.

For example, a business may require that the role which CREATEs business objects is not the same one as EDITs them. In this case, the implemented web pages may not be seen by the same roles (and people) at all.

If you implement CREATE and EDIT using common functionality but the business need is for role separation, you must still implement "role checking" before rendering the required view/partial view/etc. In such cases, separate views can be a preferred implementation.

like image 20
pixelda Avatar answered Oct 13 '22 15:10

pixelda