Unintended Updates in ASP.NET MVC

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.

  1. What are Unintended Updates in ASP.NET MVC?
  2. Example to understand Unintended Updates in ASP.NET MVC.
  3. 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.

  1. Name
  2. Gender
  3. City
  4. Salary
  5. DateOfBirth

Let’s make the “Name” field is non-editable. To achieve this change the following code in the Edit.cshtml file.

Unintended Updates in ASP.NET MVC

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

Unintended Updates in ASP.NET MVC

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

Unintended Updates in ASP.NET MVC

Then select the web view as shown below

Unintended Updates in ASP.NET MVC

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.

  1. Click on theĀ ComposerĀ tab in the fiddler
  2. Drag and drop the following URL from theĀ Web SessionsĀ window onto the Composer window.
  3. InĀ Request BodyĀ under theĀ ComposerĀ tab changeĀ NameĀ of the employee toĀ XYZ
  4. 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:
  1. The name of the method is changed fromĀ EditĀ toĀ Edit_Post
  2. 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.
  3. TheĀ idĀ of the employee that is being edited is passed as a parameter to this method.
  4. Using theĀ idĀ parameter we load the employee details (Id, Name, Gender, City, Salary & DateOfBirth) from the database.
  5. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *