Back to: ASP.NET MVC Tutorial For Beginners and Professionals
Unintended Updates in ASP.NET MVC Application
In this article, I am going to discuss Unintended Updates in the ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed How to Update a Model in ASP.NET MVC Application. We are also going to work with the same example that we worked on in our previous article. As part of this article, we are going to discuss the following pointers.
- What are Unintended Updates in ASP.NET MVC?
- Example to understand Unintended Updates in ASP.NET MVC.
- How to prevent Unintended Updates?
Let’s understand Unintended Updates with an example.Ā
At the moment, within the “Employee Edit”Ā view, we are allowing to change all of the following fields.
- Name
- Gender
- City
- Salary
- DateOfBirth
Let’s make the “Name” field is non-editable. To achieve this change the following code in the Edit.cshtml file.
Run the application and edit an employee. Notice that theĀ NameĀ of the employee is no longer rendered using a textbox. At this point, you may think that it is impossible for the user to change the name of the employee using the “Edit“Ā view. That is not true. Because of the way we have written our code tools like Fiddler and Postman can be used very easily to change any properties of theĀ “Employee“Ā object.
Using Fiddler to Post data:
Fiddler can be downloaded from the following URL
https://www.telerik.com/download/fiddler
Once you downloaded and installed the fiddler, then run fiddler. Select the Composer Tab and then select the method as GET. Provide the URL as http://localhost:54094/Employee/Edit/1 andĀ click on the execute button as shown below
In the fiddler in theĀ web sessionsĀ window, select the URL. Under theĀ “Inspectors“Ā tab we can see Request headers and responses. We will discuss more on fiddler in a later session. To see this click on the below URL
Then select the web view as shown below
Now click on theĀ “Save”Ā button on theĀ “Edit”Ā view. Notice that underĀ “Web Sessions”Ā in fiddler another request is captured for the same URL ā http://localhost:54094/Employee/Edit/1
Now without using the browser, let’ us see how to generate a post request using fiddler.
- Click on theĀ “Composer“Ā tab in the fiddler
- Drag and drop the following URL from theĀ “Web Sessions“Ā window onto the Composer window.
- InĀ “Request Body“Ā under theĀ “Composer“Ā tab changeĀ “Name“Ā of the employee toĀ “XYZ“
- Finally, clickĀ the “Execute“Ā button
Now either query the database table or navigate to theĀ “Index”Ā view and notice that the employee name is changed toĀ “XYZ”.
How to prevent unintended updates in ASP.NET MVC?
Modify theĀ “Edit” action method of EmployeeController that is decorated with [HttpPost] attribute as shown below.
[HttpPost] [ActionName("Edit")] public ActionResult Edit_Post(int id) { EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer(); Employee employee = employeeBusinessLayer.GetAllEmployess().FirstOrDefault(x => x.ID == id); UpdateModel(employee, new string[] { "ID", "Gender", "City", "Salary", "DateOfBirth" }); if (ModelState.IsValid) { employeeBusinessLayer.UpdateEmmployee(employee); return RedirectToAction("Index"); } return View(employee); }
Code Explanation:
- The name of the method is changed fromĀ “Edit“Ā toĀ “Edit_Post“
- The method is decorated with [ActionName(“Edit”)] and [HttpPost] attributes. This indicates that this method is going to respond to theĀ “Edit“Ā action when the form is posted to the server.
- TheĀ “id“Ā of the employee that is being edited is passed as a parameter to this method.
- Using theĀ “id“Ā parameter we load the employee details (Id, Name, Gender, City, Salary & DateOfBirth) from the database.
- We then callĀ UpdateModel() function. This should automatically update theĀ “Employee” object with data from the posted form. We are also passing a string array as the second parameter. This parameter specifies the list of model properties to update. This is also called including a listĀ orĀ white list. Notice that we did not include the “Name“Ā property on the list. This means even if the posted form data contains the value for theĀ “Name“Ā property it will not be used to update theĀ “Name“Ā property of theĀ “Employee“Ā object.
So, if you generated a post request using the fiddler “Name“Ā property of the “Employee”Ā object will not be updated. Alternatively to exclude properties from binding we can specify the exclude list as shown below.Ā
[HttpPost] [ActionName("Edit")] public ActionResult Edit_Post(int id) { EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer(); Employee employee = employeeBusinessLayer.GetAllEmployess().Single(x => x.ID == id); UpdateModel(employee, null, null, new string[] { "Name" }); if (ModelState.IsValid) { employeeBusinessLayer.UpdateEmmployee(employee); return RedirectToAction("Index"); } return View(employee); }
Notice that we are using a different overloaded version of theĀ UpdateModel() function. We are passing “NULL” for “prefix”Ā and theĀ “includeProperties”Ā parameters
UpdateModel<TModel>(TModelĀ model,Ā stringĀ prefix,Ā string[] includeProperties,Ā string[] excludeProperties)Ā
In the next article, I am going to discuss how to use the Bind Parameter to include and exclude properties from model binding in the ASP.NET MVC application. Here, In this article, I try to explain how unintended updates can happen in ASP.NET MVC applications and how to prevent unintended updates in ASP.NET MVC applications with examples. I would like to have your feedback. Please post your feedback, question, or comments about this article.