【问题标题】:What is ScaffoldColumn and RegularExpression attributes什么是 ScaffoldColumn 和 RegularExpression 属性
【发布时间】:2013-05-01 02:22:26
【问题描述】:

我正在努力学习MVC4,我已经来到了这个叫做验证的章节。

我开始了解DataAnnotations,它们具有非常简洁的属性来进行一些服务器端验证。在书中,他们只解释了[Required][Datatype] 属性。但是在 asp.net 网站上,我看到了一些名为 ScaffoldColumnRegularExpression 的东西。

有人能解释一下它们是什么,尽管我对RegularExpression 的作用知之甚少。 还有其他我应该知道的重要验证属性吗?

【问题讨论】:

标签: regex asp.net-mvc validation attributes


【解决方案1】:

Scaffold Column 规定在添加基于该数据模型的视图时是否应该/不应该为该列搭建脚手架。因此,例如,您的模型的 id 字段非常适合您指定 ScaffoldColumn(false) 和其他外键字段等。

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

【讨论】:

    【解决方案2】:

    你可以阅读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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 2015-11-09
      • 2014-09-02
      • 2019-06-20
      相关资源
      最近更新 更多