2013-05-07 100 views
3

我正在努力学习MVC4,我已经谈到了这一章,称为验证。什么是ScaffoldColumn和RegularExpression属性

我来了解DataAnnotations,他们有非常整洁的属性做一些服务器端验证。在书中,他们只解释了关于[Required][Datatype]属性。然而在asp.net网站上我看到了一些叫做ScaffoldColumnRegularExpression的东西。

有人可以解释他们是什么,即使我知道什么RegularExpression做什么。 也有我应该知道的任何其他重要的验证属性?

+0

http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model – Antevirus 2013-05-07 07:47:47

+0

谢谢,但我没有请参阅ScaffoldColumn和RegularExpression。我看到一些博客试图解释,但这不是很有帮助。 – Cybercop 2013-05-07 07:51:34

回答

1

脚手架栏规定,如果添加基于该数据模型的视图,它应该/不支撑该列。因此,例如,您的模型的ID字段是指定ScaffoldColumn(false)和其他外键字段等的一个很好的候选项。

我指定了一个正则表达式,如果您为该模型搭建了一个新视图,请编辑客户例如,字段上的正则表达式或正则表达式将强制输入的数据必须匹配该格式。

0

你可以阅读有关ScaffoldColumnAttribute Classhere

[MetadataType(typeof(ProductMetadata))] 
public partial class Product 
{ 

} 

public class ProductMetadata 
{ 
    [ScaffoldColumn(true)] 
    public object ProductID; 

    [ScaffoldColumn(false)] 
    public object ThumbnailPhotoFileName; 


} 

而关于RegularExpressionAttribute Class你可以阅读here

using System; 
using System.Web.DynamicData; 
using System.ComponentModel.DataAnnotations; 


[MetadataType(typeof(CustomerMetaData))] 
public partial class Customer 
{ 


} 

public class CustomerMetaData 
{ 

    // Allow up to 40 uppercase and lowercase 
    // characters. Use custom error. 
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", 
     ErrorMessage = "Characters are not allowed.")] 
    public object FirstName; 

    // Allow up to 40 uppercase and lowercase 
    // characters. Use standard error. 
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")] 
    public object LastName; 
}