In this Video, I am going to discuss how to edit a model in ASP.NET MVC Application. Please read our previous Video before proceeding to this Video as we are going to work with the same example. In our previous Video, we discussed how to update the model using UpdateModel and TryUpdateModel function. Here, in this Video, I will show you how to update a model in ASP.NET MVC application step by step.

Step1: Creating the Edit action method.
Please copy and paste the following “Edit” action method within the “EmployeeController” and include the System.Linq namespace. Here, first, we are retrieving all the employees and then finding the employees by using the Linq FirstOrDefault method. But in real-time you need to create one method in your business layer and that method should return the employee based on the id.
Points to note:
- This method is decorated with the [HttpGet] attribute. So this method only responds to HTTP get requests when editing the data.
- The Edit action method also receives the id of the employee that is being edited. This “id” is used to retrieve the employee details.
- The employee object is passed to the view
Step2: Creating the “Edit” view
Now lets add the Edit view. To do this right-click on the Edit action method and then select Add View option from the context menu and then Set
View name = “Edit”
Template = “Edit”
Model class = Employee (BusinessLayer)
Click on the “Add” button as shown below. This should add the “Edit.cshtml” view within the “Employee” folder which is within the “Views” folder.
Run the application and navigate to http://localhost:54094/Employee/Index. This page should display the list of all the employees. Click on the “Edit” link which is available on the right side of the employee. The “Edit” page should display the details of the “Employee” that is being edited as shown below.
Notice that by default “textboxes” are used for editing. It is ideal to have a drop-down list for gender rather than a textbox. To achieve this make the following changes to “Edit.cshtml”.
With the above changes in place, the complete code in Edit.cshtml view as below.
Run the application. Edit an employee, and notice that a DropDownList is used for gender as expected.
Create a stored procedure to update employee data.
Add the following “UpdateEmployee()” method to the “EmployeeBusinessLayer” class in the “BusinessLayer” project.
This method is used to update employee data to the database table.
Creating the Edit Post Method:
Please copy and paste the following “Edit” (HttpPost) action method in “EmployeeController“.
Code Explanation:
- The above Edit action method is decorated with the [HttpPost] attribute. So this method only responds to HTTP post requests when updating data.
- This method receives the modified “Employee” object as a parameter. This object is then passed to UpdateEmmployee() method which updates the employee details. After the employee details are saved the user is redirected to “Index” action.
- If there are model validation errors none of the code in the IF block gets executed. In this case, the user stays on the “Edit” view. Since we are passing the “Employee” object to the “Edit” view the user gets to see the validation errors. This allows him to fix those errors and re-submit the view.