Search code examples
asp.net-mvcmodelstateviewdata

Is there a difference between ModelState and ViewData.ModelState?


In some ASP.NET MVC code examples, I see references to ModelState.IsValid, and in other examples I see references to ViewData.ModelState.IsValid.

In my initial research, I see:

  • ModelState is a public property in Controller class.
  • ViewData is a public property in ControllerBase class.

Is there a difference between the two? Why have both?


Solution

  • They reference the exact same thing, if you look at the code for System.Web.Mvc.Controller class you will see the implementation for ModelState is:

       public ModelStateDictionary ModelState
        {
          get
          {
            return this.ViewData.ModelState;
          }
        }
    

    I would say it is simply there for easy of use in your own Controller implementations.