如何Html.ValidationSummary指定数据标注错误的顺序 [英] How to specify order of Data Annotation errors in Html.ValidationSummary

查看:128
本文介绍了如何Html.ValidationSummary指定数据标注错误的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的表格上显示错误与使用

 <%= Html.ValidationSummary(请检查下面的错误)%>

我的域对象从一个基类继承,我发现了基类的数据标注属性在列表的底部显示。这违背它们出现在我的表的顺序。

有没有指定什么样的顺序应该显示错误的方法吗?

例如:

 公共类ClassA的{[必填]公共字符串AProperty; }
公共类ClassB的:ClassA的{[必填]公共字符串BProperty; }

我的形式(强类型的ClassB的观点):

  AProperty:LT;%= Html.TextBoxFor(M = GT; m.AProperty)%GT;
BProperty:其中;%= Html.TextBoxFor(M => m.BProperty)%GT;

验证错误的出现:

 是必需的BProperty。
该AProperty是必需的。


解决方案

不。使用反射来获取所有DataAnnotations,他们总是出现的顺序属性似乎与到 typeof运算(MagicSocks)的调用.GetTYpe()的GetProperties()。你的情况我是pretty确保派生类的属性将总是先基本类型属性出现。

您必须编写自己的助手和我们自己的属性,在你选择的顺序来显示验证错误。

I'm displaying errors on my form with the use of

<%= Html.ValidationSummary("Please review the errors below") %>

My domain object inherits from a base class and I am finding that the base class data annotation properties are being displayed at the bottom of the list. This goes against the order in which they appear in my form.

Is there any way of specifying what order the errors should be displayed?

Example:

public class ClassA { [Required]public string AProperty; }
public class ClassB : ClassA { [Required]public string BProperty; }

My form (strongly typed view of ClassB):

AProperty: <%= Html.TextBoxFor(m => m.AProperty) %>
BProperty: <%= Html.TextBoxFor(m => m.BProperty) %>

Validation errors appear as:

The BProperty is required.
The AProperty is required.

解决方案

Nope. Reflection is used to get all the DataAnnotations and they always appear in the order the properties would appear with a call to typeof(MagicSocks).GetTYpe().GetProperties(). In your case I'm pretty sure derived class properties will always appear before base type properties.

You have to write your own helper and our own attributes to display the validation errors in the order you choose.

这篇关于如何Html.ValidationSummary指定数据标注错误的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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