我想以dd-mmm-yyyy的形式执行日期。我将以下帖子用作指导:

http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx

问题:
ValidationMessageFor文本将不会显示(在回发和客户端)。有什么建议吗?

更新:

通过包含@ Darin-Demintrov的答案,此问题中的代码现在可以使用了。请注意,确认消息在选择日期后仍然存在。要解决此问题,必须按照下面的答案处理DatePicker onChange()事件。

查看模型属性:

    [Required(ErrorMessage = "* required")]
    [Display(Name = "Event Date")]
    [PpcDate]
    public DateTime EventDate { get; set; }


PpcDate ValidationAttribute类:

public class PpcDateAttribute : ValidationAttribute, IClientValidatable, IMetadataAware
{
    /// <summary>
    /// Any valid DateTime is fine;
    /// the regex verification only happens on the client
    /// </summary>
    public override bool IsValid(object value)
    {
        if (value == null || (value is DateTime))
            return true;
        else
            return false;
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format("{0} must be in the form dd-mmm-yyyy", name);
    }

    #region IClientValidatable Members

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(metadata.DisplayName),
            ValidationType = "ppcdate"
        };
    }

    #endregion

    #region IMetadataAware Members

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        metadata.DataTypeName = "Date";
    }

    #endregion
}


风景:

@Html.EditorFor(model => model.EventDate)
@Html.ValidationMessageFor(model => model.EventDate)


和javascript联播:

(function ($) {
    // The validator function
    $.validator.addMethod('ppcdate', function (value) {
        if (!value) {
            return true;
        } else {
            return /^\d{2}-\w{3}-\d{4}$/.test(value);
        }

    });

    // The adapter to support ASP.NET MVC unobtrusive validation
    $.validator.unobtrusive.adapters.add('ppcdate', [], function (options) {

        // EDIT: Add next line per Darin's answer below.
        options.rules['ppcdate'] = true;

        if (options.message) {
            options.messages['ppcdate'] = options.message;
        }
    });
} (jQuery));


生成的html肯定看起来正确吗?

请注意data-val-ppcdate属性按预期显示...

<div class="t-widget t-datepicker">
  <div class="t-picker-wrap">
    <input autocomplete="off" class="t-input valid" data-val="true" data-val-ppcdate="Event Date must be in the form dd-mmm-yyyy" data-val-required="* required" id="EventDate" name="EventDate" value="28-Sep-2011" type="text">
    <span class="t-select">
      <span class="t-icon t-icon-calendar" title="Open the calendar">Open the calendar</span>
    </span>
  </div>
</div>
<span class="field-validation-valid" data-valmsg-for="EventDate" data-valmsg-replace="true"></span>


有什么建议吗?

最佳答案

您忘记注册规则,并且您的自定义ppcdate验证方法永远不会触发:

$.validator.unobtrusive.adapters.add('ppcdate', [], function (options) {
    if (options.message) {
        // This is what associates the adapter with the custom validation method
        options.rules['ppcdate'] = options.params;
        options.messages['ppcdate'] = options.message;
    }
});


这是完整的example

09-17 17:02