Fix “The Model of Type Could Not Be Updated” from UpdateModel in ASP.NET MVC 5

Quick fix. When this exception comes from Controller.UpdateModel, switch the failing path to TryUpdateModel and read the field-level ModelState errors. Return the edit form with those errors and save only after binding and validation succeed.

This guide applies to ASP.NET MVC 5 on .NET Framework (System.Web.Mvc).

Find the field that failed

UpdateModel delegates to the binder and throws when the update is unsuccessful. A quantity posted as text, a missing required value, or a field that fails a range check can all land in ModelState. Inspect the keys in ModelState, then read the Errors for each invalid key. Do this locally or in development logs that avoid printing submitted personal data.

Bind to an edit model and use an allowlist

This example assumes the edit page handles an order already loaded through an owner-scoped repository. Configure the Razor input names as Order.Quantity and Order.Reference when using the Order prefix. The shape submitted by the browser contains no owner or pricing fields.

public class OrderEditInput
{
    [System.ComponentModel.DataAnnotations.Range(1, 100)]
    public int Quantity { get; set; }

    [System.ComponentModel.DataAnnotations.StringLength(50)]
    public string Reference { get; set; }
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id)
{
    var order = orders.FindForUser(id, User.Identity.Name);
    if (order == null)
        return HttpNotFound();

    var input = new OrderEditInput();
    if (!TryUpdateModel(input, "Order", new[] { "Quantity", "Reference" }))
        return View(input);

    order.Quantity = input.Quantity;
    order.Reference = input.Reference;
    orders.Save();
    return RedirectToAction("Details", new { id });
}

Keep rendering and binding in agreement

Render Html.ValidationSummary or validation messages for the fields. A model binder prefix mismatch may leave an optional field at its default without an obvious HTML error. Compare the actual posted keys with the ModelState keys and the prefix you passed to the binder. Rebuild any dropdown lists before rendering the form again.

Verify validation and persistence

Submit an invalid number and confirm that the edit form displays a field error and no change is saved. Submit a valid quantity and verify the expected redirect and stored values. Include an integration check that additional posted fields cannot influence values outside the allowlist.

Related reading

Comments