MVC DataAnnotation 在 MVC3 中渲染错误的 HTML

发布于 2024-10-31 23:08:29 字数 555 浏览 1 评论 0原文

我有一个简单的模型对象,如下所示:

[DataMember]
[DataType(DataType.Date)]
public DateTime WeekEndingDate { get; set; }

和一个执行此操作的视图页面,

 @Html.TextBoxFor(model => model.CashEntry.WeekEndingDate)

但我的问题是它将其呈现为标记:

<input data-val="true" data-val-required="The WeekEndingDate field is required." id="CashEntry_WeekEndingDate" name="CashEntry.WeekEndingDate" type="text" value="" />

当真正的 data-val-required 应该是 data-val-date 时。这也搞砸了日期字段页面上的验证。对于为什么会发生这种情况有什么想法吗?

I have a straight forward model object that looks like this:

[DataMember]
[DataType(DataType.Date)]
public DateTime WeekEndingDate { get; set; }

And a View Page That Does this

 @Html.TextBoxFor(model => model.CashEntry.WeekEndingDate)

But My Problem is that it renders this as the markup:

<input data-val="true" data-val-required="The WeekEndingDate field is required." id="CashEntry_WeekEndingDate" name="CashEntry.WeekEndingDate" type="text" value="" />

When really data-val-required should be data-val-date . This is screwing up the validation also on the page of the date field. Any thoughts as to why this may be happening?

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

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

发布评论

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

评论(1

无名指的心愿 2024-11-07 23:08:29

我担心 ASP.NET MVC 3 永远不会创建 data-val-date,这里是可能创建的属性列表:

  • data-val-number
  • data-val-equalto
  • data-val-range
  • data-val-regex
  • data-val -remote
  • data-val-required
  • data-val-length

您可以为 DataType 属性创建额外的 ModelValidatorProvider,如下所示:

public class ClientDataTypeNameModelValidatorProvider : ModelValidatorProvider
{
    public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
    {
        if (metadata == null)
            throw new ArgumentNullException("metadata");

        if (context == null)
            throw new ArgumentNullException("context");

        if (metadata.DataTypeName == "Date")
            yield return new DateModelValidator(metadata, context);
    }

    internal sealed class DateModelValidator : ModelValidator
    {
        public DateModelValidator(ModelMetadata metadata, ControllerContext controllerContext)
            : base(metadata, controllerContext)
        { }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            ModelClientValidationRule rule = new ModelClientValidationRule()
            {
                ValidationType = "date",
                ErrorMessage = "[Put your error message here]"
            };

            return new ModelClientValidationRule[] { rule };
        }

        public override IEnumerable<ModelValidationResult> Validate(object container)
        {
            // No server-side validation logic
            return Enumerable.Empty<ModelValidationResult>();
        }
    }
}

并在 Global.asax 中注册它,如下所示:

ModelValidatorProviders.Providers.Add(new ClientDataTypeNameModelValidatorProvider());

这应该为您提供对 DataType.Date 的客户端验证支持,但您必须记住它仅适用于 jQuery 验证,因为 Microsoft 验证没有日期规则。

I'm affraid ASP.NET MVC 3 never creates data-val-date, here is list of attributes that might be created:

  • data-val-number
  • data-val-equalto
  • data-val-range
  • data-val-regex
  • data-val-remote
  • data-val-required
  • data-val-length

You can create additional ModelValidatorProvider for DataType attribute like this:

public class ClientDataTypeNameModelValidatorProvider : ModelValidatorProvider
{
    public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
    {
        if (metadata == null)
            throw new ArgumentNullException("metadata");

        if (context == null)
            throw new ArgumentNullException("context");

        if (metadata.DataTypeName == "Date")
            yield return new DateModelValidator(metadata, context);
    }

    internal sealed class DateModelValidator : ModelValidator
    {
        public DateModelValidator(ModelMetadata metadata, ControllerContext controllerContext)
            : base(metadata, controllerContext)
        { }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            ModelClientValidationRule rule = new ModelClientValidationRule()
            {
                ValidationType = "date",
                ErrorMessage = "[Put your error message here]"
            };

            return new ModelClientValidationRule[] { rule };
        }

        public override IEnumerable<ModelValidationResult> Validate(object container)
        {
            // No server-side validation logic
            return Enumerable.Empty<ModelValidationResult>();
        }
    }
}

And register it in your Global.asax like this:

ModelValidatorProviders.Providers.Add(new ClientDataTypeNameModelValidatorProvider());

This should give you client validation support for DataType.Date, but you must remember that it will work only with jQuery validation as Microsoft validation doesn't have rule for date.

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