我如何在ASP.NET MVC模型状态错误的收藏? [英] How do I get the collection of Model State Errors in ASP.NET MVC?

查看:125
本文介绍了我如何在ASP.NET MVC模型状态错误的收藏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何得到错误的集合中的看法?

我不希望使用HTML辅助验证摘要或验证消息。相反,我要检查错误,并在特定的格式,如果任何显示。同样在输入控件我想检查特定属性的错误,并添加一个类来输入。

P.S。我使用的是星火视图引擎,但这个想法应该是一样的。

所以,我想我可以做这样的事情...

 <如果条件=$ {ModelState.Errors.Count大于0}>
  DispalyErrorSummary()
< /若>....并且...<输入类型=文本值=$ {} Model.Name
       {ModelState.Errors类= [名称]!=的String.Empty}错误/>....

或者类似的东西。

更新

我的最终解决方案是这样的:

 <输入类型=文本值=$ {} ViewData.Model.Name
       类=文本错误{ViewData.ModelState.IsValid&放大器;?!&安培;
                           ViewData.ModelState [名称] Errors.Count()> 0}
       ID =名称NAME =名称/>

这只会增加出错的CSS类,如果该属性有错误。


解决方案

 <%ViewData.ModelState.IsValid%GT;

 <%ViewData.ModelState.Values​​.Any(X => x.Errors.Count> = 1)%GT;

和特定属性...

 <%ViewData.ModelState [属性]错误%GT。 //注意这将返回集合

How do I get the collection of errors in a view?

I don't want to use the Html Helper Validation Summary or Validation Message. Instead I want to check for errors and if any display them in specific format. Also on the input controls I want to check for a specific property error and add a class to the input.

P.S. I'm using the Spark View Engine but the idea should be the same.

So I figured I could do something like...

<if condition="${ModelState.Errors.Count > 0}">
  DispalyErrorSummary()
</if>

....and also...

<input type="text" value="${Model.Name}" 
       class="?{ModelState.Errors["Name"] != string.empty} error" />

....

Or something like that.

UPDATE

My final solution looked like this:

<input type="text" value="${ViewData.Model.Name}" 
       class="text error?{!ViewData.ModelState.IsValid && 
                           ViewData.ModelState["Name"].Errors.Count() > 0}" 
       id="Name" name="Name" />

This only adds the error css class if this property has an error.

解决方案

<% ViewData.ModelState.IsValid %>

or

<% ViewData.ModelState.Values.Any(x => x.Errors.Count >= 1) %>

and for a specific property...

<% ViewData.ModelState["Property"].Errors %> // Note this returns a collection

这篇关于我如何在ASP.NET MVC模型状态错误的收藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆