EditorFor 和 StringLength DataAnnotations

发布于 2024-11-29 19:30:21 字数 557 浏览 3 评论 0原文

我的模型上有以下属性

        [Display(Name = "MyProperty")]
        [StringLength(10)]
        public string MyProperty
        {
            get;set;
        }

和以下 EditorFor 模板

<%@ Page Language="C#" MasterPageFile="Template.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Data" runat="server">
    <%= Html.TextBox("", Model)%>
</asp:Content>

我遇到的问题是 StringLength 属性(可以理解)没有设置为限制文本框大小。我的答案是我应该如何获取要在模板中设置的属性?

谢谢

I have the following property on my Model

        [Display(Name = "MyProperty")]
        [StringLength(10)]
        public string MyProperty
        {
            get;set;
        }

and the following EditorFor template

<%@ Page Language="C#" MasterPageFile="Template.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Data" runat="server">
    <%= Html.TextBox("", Model)%>
</asp:Content>

The problem I have is that the StringLength property (understandably), isn't being set to limit the textbox size. My answer is how should I be obtaining the attributes to set in my template?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏璃陌 2024-12-06 19:30:21

如果您使用的是 ASP.NET MVC 3 或更高版本,则接受的答案不起作用。您需要更改用于获取值的键:

IEnumerable<ModelValidator> validators = ModelValidatorProviders.Providers
    .GetValidators(ViewData.ModelMetadata, ViewContext);

ModelClientValidationRule rule = validators.SelectMany(v => 
    v.GetClientValidationRules()
).FirstOrDefault(m => m.ValidationType == "length");

if (rule != null && rule.ValidationParameters.ContainsKey("max")) {
    var maxLength = rule.ValidationParameters["max"];
}

The accepted answer doesn't work if you are using ASP.NET MVC 3 or later. You need to change the keys used to get the value:

IEnumerable<ModelValidator> validators = ModelValidatorProviders.Providers
    .GetValidators(ViewData.ModelMetadata, ViewContext);

ModelClientValidationRule rule = validators.SelectMany(v => 
    v.GetClientValidationRules()
).FirstOrDefault(m => m.ValidationType == "length");

if (rule != null && rule.ValidationParameters.ContainsKey("max")) {
    var maxLength = rule.ValidationParameters["max"];
}
顾铮苏瑾 2024-12-06 19:30:21

元数据属性和验证属性之间存在差异。 StringLengthAttribute 是一个验证属性,因此您无法从 ModelMetadata 获取它。

幸运的是,Wayne Brantley 已经完成了艰苦的工作 。以下是他获取验证规则的方式:

IEnumerable<ModelValidator> validators = ModelValidatorProviders.Providers.GetValidators(ViewData.ModelMetadata, ViewContext);
ModelClientValidationRule rule = validators.SelectMany(v => v.GetClientValidationRules()).FirstOrDefault(m => m.ValidationType == "stringLength");
if (rule != null && rule.ValidationParameters.ContainsKey("maximumLength"))
{
   var maxLength = rule.ValidationParameters["maximumLength"];
}

注意:如果您使用的是 ASP.NET MVC 3 或更高版本,则需要将 stringLength 更改为 length 和 maximumLengthmax

There is a difference between metadata attributes and validation attributes. StringLengthAttribute is a validation attribute, so you can't get it from ModelMetadata.

Luckily, Wayne Brantley has done the hard work. Here is how he gets the validation rules:

IEnumerable<ModelValidator> validators = ModelValidatorProviders.Providers.GetValidators(ViewData.ModelMetadata, ViewContext);
ModelClientValidationRule rule = validators.SelectMany(v => v.GetClientValidationRules()).FirstOrDefault(m => m.ValidationType == "stringLength");
if (rule != null && rule.ValidationParameters.ContainsKey("maximumLength"))
{
   var maxLength = rule.ValidationParameters["maximumLength"];
}

Note: if you are using ASP.NET MVC 3 or later, you will need to change stringLength to length and maximumLength to max.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文