你怎么做网页表单模型验证? [英] How do you do web forms model validation?
问题描述
我们有三个层次的应用程序:用户界面,业务和数据。数据层的房屋实体框架v4和自动生成我们的实体对象。我创建了一个哥们类实体 VENDORINFO
:
We have an application with three layers: UI, Business, and Data. The data layer houses Entity Framework v4 and auto-generates our entity objects. I have created a buddy class for the entity VendorInfo
:
namespace Company.DataAccess
{
[MetadataType(typeof(VendorInfoMetadata))]
public partial class VendorInfo
{
}
public class VendorInfoMetadata
{
[Required]
public string Title;
[Required]
public string Link;
[Required]
public string LinkText;
[Required]
public string Description;
}
}
我想这个验证泡到用户界面,包括分配给他们的自定义验证消息。在MVC,这是小菜一碟,但在Web表单里我不知道从哪里开始。什么是利用模型验证在asp.net web表单的最佳方式?
I want this validation to bubble up to the UI, including custom validation messages assigned to them. In MVC this is a piece of cake but in web forms I have no clue where to begin. What is the best way to utilize model validation in asp.net web forms?
我没有找到<一个href=\"http://www.$c$cproject.com/KB/validation/Data_Annotations_with_ASP.aspx?msg=3603632&display=Mobile\"相对=nofollow>文章说明如何建立一个服务器控制它,但我似乎无法得到它的工作。它编译,甚至可以识别控制,但我永远不能得到它开火。
I did find an article that explains how to build a server control for it, but I can't seem to get it working. It compiles and even recognizes the control but I can never get it to fire.
任何想法?
谢谢大家。
推荐答案
我解决它。这样看来,在<一href=\"http://www.$c$cproject.com/KB/validation/Data_Annotations_with_ASP.aspx?msg=3603632&display=Mobile\"相对=nofollow>服务器控件,我发现没有被设计通过MetadataType属性读取一个哥们类领域。我修改了code,寻找其验证的哥们类而不是实体类属性。
I solved it. It would appear that the server control I found was not designed to read fields in a buddy class via the MetadataType attribute. I modified the code to look for its validation attributes in the buddy class rather than the entity class itself.
下面是链接的服务器控件的修改版本:
Here is the modified version of the linked server control:
[DefaultProperty("Text")]
[ToolboxData("<{0}:DataAnnotationValidator runat=server></{0}:DataAnnotationValidator>")]
public class DataAnnotationValidator : BaseValidator
{
#region Properties
/// <summary>
/// The type of the source to check
/// </summary>
public string SourceTypeName { get; set; }
/// <summary>
/// The property that is annotated
/// </summary>
public string PropertyName { get; set; }
#endregion
#region Methods
protected override bool EvaluateIsValid()
{
// get the type that we are going to validate
Type source = GetValidatedType();
// get the property to validate
FieldInfo property = GetValidatedProperty(source);
// get the control validation value
string value = GetControlValidationValue(ControlToValidate);
foreach (var attribute in property.GetCustomAttributes(
typeof(ValidationAttribute), true)
.OfType<ValidationAttribute>())
{
if (!attribute.IsValid(value))
{
ErrorMessage = attribute.ErrorMessage;
return false;
}
}
return true;
}
private Type GetValidatedType()
{
if (string.IsNullOrEmpty(SourceTypeName))
{
throw new InvalidOperationException(
"Null SourceTypeName can't be validated");
}
Type validatedType = Type.GetType(SourceTypeName);
if (validatedType == null)
{
throw new InvalidOperationException(
string.Format("{0}:{1}",
"Invalid SourceTypeName", SourceTypeName));
}
IEnumerable<MetadataTypeAttribute> mt = validatedType.GetCustomAttributes(typeof(MetadataTypeAttribute), false).OfType<MetadataTypeAttribute>();
if (mt.Count() > 0)
{
validatedType = mt.First().MetadataClassType;
}
return validatedType;
}
private FieldInfo GetValidatedProperty(Type source)
{
FieldInfo field = source.GetField(PropertyName);
if (field == null)
{
throw new InvalidOperationException(
string.Format("{0}:{1}",
"Validated Property Does Not Exists", PropertyName));
}
return field;
}
#endregion
}
这code的只有的看在哥们类。如果你想它来检查一个实际的类,然后它的哥们类,你就必须相应地修改它。我没有理会这样做,因为通常如果你使用的是哥们类属性验证,那是因为你不能够在主要实体类(例如实体框架)。使用属性
This code only looks in the buddy class. If you want it to check an actual class and then its buddy class, you'll have to modify it accordingly. I did not bother doing that because usually if you are using a buddy class for validation attributes it's because you are not able to use the attributes in the main entity class (e.g. Entity Framework).
这篇关于你怎么做网页表单模型验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!