As @shenku answered before, ViewData
and ModelState
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;
}
}
and @JimmiTh said,
although the main use of ModelState
from an "end developer"'s perspective is in the controller, ViewData
is used as a container for all data that's communicated between the controller and the view. Which is why it also needs to include ModelState
- because, although you'd rarely use it directly in the view, ModelState
is where e.g. many of the HtmlHelper
methods actually get the values from by default when rendering the view from a POST action - rather than Model.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…