Should I use the same controller and view to edit and create models in ASP.NET MVC?

Do I need to use the same controller and view to edit and create models in ASP.NET MVC, or do I need to create a view and controller action to create and a view and controller action to edit?

The edit view is likely to be different - it doesn't always make sense that the UI for editing an object is the same as the view for creation.

By separating the views, I will avoid many "if statements" to determine if I am editing or creating ...

Thoughts?

0


source to share


2 answers


The guidelines I've seen show that the views are split with the common elements contained in the control to stay dry.

It seems logical to follow the same approach with the controller - reusing common parts where necessary.



This view is reflected here How to properly reuse edit / new views in Asp.NET MVC as the question is very similar.

+2


source


AFAIK recommended: (Face used in example)

use one controller to handle both cases

There are four actions in this controller:

  • New ()
  • Change (int personId)
  • Create (Person p)
  • Update (Person p)

Two views, Person / New.aspx and person / Edit.aspx



both views contain a form that is placed in the corresponding actions:

  • Create → Create
  • Edit -> Update

You now have two options, either implement the form content twice (in each of the views), or implement the actual form in PersonForm.ascx and use partial rendering to render the form content.

Which way to choose the last one depends on whether the forms are more or less the same (go for general control) or if they should be different (implement two different)

If it's just a matter of different layout in new formats, you can just link to different css files and let css handle the differences

0


source







All Articles