0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.