Home > Mobile >  Do we really need to different views for Add and Edit in ASP.Net MVC?
Do we really need to different views for Add and Edit in ASP.Net MVC?

Time:09-18

Why we have two different views in ASP.NET MVC for Add and Edit? What is the advantage of this? Knowing that this will make us having duplicate code in the views? And does merging the two views in one view break the standards of the ASP.NET MVC?

CodePudding user response:

They are the defaults that are provided to get you up and running quickly with a CRUD application. It's not necessary, nor does it break convention to have it done on the same page.

If you're after something quick and easy, code scaffolding is great but quickly becomes unwieldy.

If it's not what you need or it's too basic, simply write your own actions/views.

CodePudding user response:

I have save many written code for the merge, even its working fine, If you merge Edit and Create View in single View. Just make sure you should need to keep in mind some important things.

  1. PrimaryKey ID always keep in Hidden Textbox field. (which is useful for identity this form is Edit Mode or Create Mode)

  2. Its good idea to have some controller for single form, you dont need to write another controller for single form postback.

  3. One of the key process is check if(string.IsNullOrWhiteSpace(model.id)) before create and edit services.

Here is code for controller for separation.

public  void CreateAndEditEmployee(Employee model)
{
    if (string.IsNullOrWhiteSpace(model.EmployeeId))
    {
        // Create Employee Mode
    }
    else
    {
        // Edit Employee Mode
    }
}
  • Related