ModelState

noun

say it: “MOD-ul-stayt” /ˈmɒdəlsteɪt/

The place where ASP.NET Core records whether a request's values bound and passed validation, with an error for each field.

What is ModelState in ASP.NET Core?

ModelState — Where ASP.NET Core records how model binding and validation went for a request. ModelState.IsValid is false if any value didn't fit or broke a rule, and the view can show each error beside its field.

[HttpPost]
public IActionResult Create(Product product)
{
    if (!ModelState.IsValid) return View(product); // show the form with its errors
    productDao.Add(product);
    return RedirectToAction("Index");
}

In a sentence

  • “The action checks ModelState.IsValid and shows the form again when the price is missing.”

Level: Journeyman · More ASP.NET Core terms at this level

Updated