Asp.net mvc 3 ASP.NET MVC启用自定义验证属性

Asp.net mvc 3 ASP.NET MVC启用自定义验证属性,asp.net-mvc-3,validation,sitecore,data-annotations,Asp.net Mvc 3,Validation,Sitecore,Data Annotations,我正在尝试为ASP.NET MVC项目创建自己的模型验证属性。我遵循了来自的建议,但看不到如何获取@Html.EditorFor()来识别我的自定义属性。是否需要在web.config中的某个位置注册自定义属性类?对此的评论似乎提出了同样的问题 仅供参考,我之所以创建自己的属性,是因为我想从Sitecore检索字段显示名称和验证消息,而不是真的想创建一个包含大量静态方法来表示每个文本属性的类,如果要使用 public class MyModel { [DisplayName("Some

我正在尝试为ASP.NET MVC项目创建自己的模型验证属性。我遵循了来自的建议,但看不到如何获取
@Html.EditorFor()
来识别我的自定义属性。是否需要在web.config中的某个位置注册自定义属性类?对此的评论似乎提出了同样的问题

仅供参考,我之所以创建自己的属性,是因为我想从Sitecore检索字段显示名称和验证消息,而不是真的想创建一个包含大量静态方法来表示每个文本属性的类,如果要使用

public class MyModel
{
    [DisplayName("Some Property")]
    [Required(ErrorMessageResourceName="SomeProperty_Required", ErrorMessageResourceType=typeof(MyResourceClass))]
    public string SomeProperty{ get; set; }
}

public class MyResourceClass
{
    public static string SomeProperty_Required
    {
        get { // extract field from sitecore item  }
    }

    //for each new field validator, I would need to add an additional 
    //property to retrieve the corresponding validation message
}

这个问题在这里得到了回答:

为了使自定义验证器属性正常工作,您需要注册它。这可以通过以下代码在Global.asax中完成:

public void Application_Start()
{
    System.Web.Mvc.DataAnnotationsModelValidatorProvider.RegisterAdapter(
        typeof (MyNamespace.RequiredAttribute),
        typeof (System.Web.Mvc.RequiredAttributeAdapter));
}
(如果您正在使用,可以将上述代码放入
App\u Start
文件夹中的启动类。)

我的自定义属性类如下所示:

public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
    private string _propertyName;

    public RequiredAttribute([CallerMemberName] string propertyName = null)
    {
        _propertyName = propertyName;
    }

    public string PropertyName
    {
        get { return _propertyName; }
    }

    private string GetErrorMessage()
    {
        // Get appropriate error message from Sitecore here.
        // This could be of the form "Please specify the {0} field" 
        // where '{0}' gets replaced with the display name for the model field.
    }

    public override string FormatErrorMessage(string name)
    {
        //note that the display name for the field is passed to the 'name' argument
        return string.Format(GetErrorMessage(), name);
    }
}

这个问题在这里得到了回答:

为了使自定义验证器属性正常工作,您需要注册它。这可以通过以下代码在Global.asax中完成:

public void Application_Start()
{
    System.Web.Mvc.DataAnnotationsModelValidatorProvider.RegisterAdapter(
        typeof (MyNamespace.RequiredAttribute),
        typeof (System.Web.Mvc.RequiredAttributeAdapter));
}
(如果您正在使用,可以将上述代码放入
App\u Start
文件夹中的启动类。)

我的自定义属性类如下所示:

public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
    private string _propertyName;

    public RequiredAttribute([CallerMemberName] string propertyName = null)
    {
        _propertyName = propertyName;
    }

    public string PropertyName
    {
        get { return _propertyName; }
    }

    private string GetErrorMessage()
    {
        // Get appropriate error message from Sitecore here.
        // This could be of the form "Please specify the {0} field" 
        // where '{0}' gets replaced with the display name for the model field.
    }

    public override string FormatErrorMessage(string name)
    {
        //note that the display name for the field is passed to the 'name' argument
        return string.Format(GetErrorMessage(), name);
    }
}

对于acepts字符串,没有像您现在这样的编辑器过载,哎呀,有点混淆了。这些是可以传递到属性构造函数中的属性。现在编辑了我的问题。听起来Sitecore标记在这里不相关吗?对于acepts字符串,Editor没有过载,就像你正在做的那样,哎呀,有点混淆了。这些是可以传递到属性构造函数中的属性。现在编辑了我的问题。听起来好像Sitecore标签在这里不相关?