What is the point of the key parameter in ModelState.AddModelError in ASP.NET MVC?

后端 未结 4 1659
孤城傲影
孤城傲影 2021-02-06 20:29

I\'ve added validation checks in my controller that modify the ModelState if the validation fails.

For example:



        
4条回答
  •  深忆病人
    2021-02-06 21:33

    The Key is used by the ValidationMessage HTML Helper to know the exact error message to display.

    Example:

    <%=Html.TextBox("Name") %> 
    <%=Html.ValidationMessage("Name") %>

    the ValidationMessage helper will display the message that has the key "Name" in the ModelState dictionary.

提交回复
热议问题