modelstate

How to get all Errors from ASP.Net MVC modelState?

最后都变了- 提交于 2019-11-26 02:40:14
I want to get all the error messages out of the modelState without knowing the key values. Looping through to grab all the error messages that the ModelState contains. How can I do this? Oren Trutner foreach (ModelState modelState in ViewData.ModelState.Values) { foreach (ModelError error in modelState.Errors) { DoSomethingWith(error); } } See also How do I get the collection of Model State Errors in ASP.NET MVC? . mmutilva Using LINQ : IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors); Building on the LINQ verison, if you want to join all the error messages into

Asp.net MVC ModelState.Clear

和自甴很熟 提交于 2019-11-26 01:32:00
问题 Can anyone give me a succinct definition of the role of ModelState in Asp.net MVC (or a link to one). In particular I need to know in what situations it is necessary or desirable to call ModelState.Clear() . Bit open ended huh ... sorry, I think it might help if tell you what I\'m acutally doing: I have an Action of Edit on a Controller called \"Page\". When I first see the form to change the Page\'s details everything loads up fine (binding to a \"MyCmsPage\" object). Then I click a button