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

后端 未结 4 1656
孤城傲影
孤城傲影 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:25

    Sorry to Necropost. The above answers didn't have this detail that I thought was useful (it was what I was looking for!!)

    In order to create 'Model Wide' Validation errors - then you simply add string.Empty as your Key.

    e.g.

    ModelState.AddModelError(string.Empty, "This is my Model Level Message");
    

    Thanks to : http://www.tutorialsteacher.com/mvc/htmlhelper-validationsummary for the tip.

提交回复
热议问题