获取视图中的所有的ModelState错误 [英] Get all ModelState Errors on view
问题描述
在我的控制器我添加一些错误的ModelState。
所以,当我使我的看法,我想所有的这些错误,并更改包含一个错误字段标签的颜色。
At my Controller I add some ModelState Errors. So, when I render my View, I want to get all these Errors and change the color of the labels of the fields that contain a error.
所以,我想我需要获得所有的ModelState错误,得到字段名,然后更改颜色。这是一个很好的方式?
So, I think I need to get all the ModelState Errors, get the field names and then change the color. This is the a good way?
我怎样才能在我看来的ModelState错误?
How can I get the ModelState Errors in my view?
推荐答案
您可以通过 ViewData.ModelState
访问它。
如果你需要更多的控制对你的看法错误,你可以使用
You can access it through ViewData.ModelState
.
If you need more control with errors on your view you can use
ViewData.ModelState.IsValidField(name_of_input)
或获得的输入列表,像这样的错误:
or get a list of inputs with errors like this:
var errors = ViewData.ModelState.Where(n => n.Value.Errors.Count > 0).ToList();
这篇关于获取视图中的所有的ModelState错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!