English 中文(简体)
MVC 3 ValidationMessageFor() 否
原标题:MVC 3 ValidationMessageFor() Does Not Appear

我愿以d-mmm-yyyyy的形式强制执行日期。 Im利用以下员额作为指南:

rel=“nofollow”http://blogs.msdn.com/b/stuleeks/archive/01)/asp-net-vc-3-inting-with-jryque-date-pi-pied.

Problem: The ValidationMessageFor text will not display (both on postback and client side). 任何建议?

<>Update:

《刑法》中包含“Darin-Demintrov”的答复,现在该法典正在发挥作用。 请注意,验证电文在抽取日期后一直有效。 为了解决这一问题,我必须按下面的回答处理Change(Change)(日期)事件。

View model property:

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

<>PpcDate ValidationAttribute par:

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
}

www.un.org/Depts/DGACM/index_spanish.htm 观点:

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

www.un.org/Depts/DGACM/index_spanish.htm j:

(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));

www.un.org/Depts/DGACM/index_spanish.htm 由此得出的“html”肯定是正确的?

Note that the data-val-ppcdate attribute appears as expected...

<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>

任何建议?

最佳回答

You forgot to register the rule and your custom ppcdate validation method never fires:

$.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;
    }
});

Here s a full example.

问题回答

Load up fiddler and see if You CSS file is notloading. 我知道,如果CSS确实装上了一定负荷,那么我就不得不假设这一点同样适用于个人验证项目。

Darin的答复解释了为什么该规则没有发射。 另外一个qui点是,当Change(Change)事件日报发生火灾时,我不得不强行确认。 否则,只有在用户点击或标的直线<>/em>日期投入时才进行验证。 如果他们开放并关闭日期,则验证信息仍然有效,即使日期有效。 I m 采用Telerik控制,因此,它喜欢:

www.un.org/Depts/DGACM/index_spanish.htm 日期:ascx案 (Date control):

<%= Html.Telerik().DatePickerFor(m => m)
    .Format("dd-MMM-yyyy")
    .Value(ViewData["selectedDate"] as DateTime?)
    .ClientEvents(e => { e.OnChange("PPC_validateTelerikDate"); })
%>

http://www.ohchr.org。

/*****************************************************************************
    PPC_validateTelerikDate()
    Force validation to run on the target input.  
    This function allows one to validate when the date picker changes.
    Otherwise, validation only runs when clicking the input element directly
    or tabbing through.
*****************************************************************************/
function PPC_validateTelerikDate(e) {
    // get the form validation object
    var val = $(e.target.form).validate();
    // now validate the target
    val.element($(e.target));
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!