C# – unobtrusive client validation rules must be unique

asp.net-mvc-5cdata-annotationsvalidation

Environment: Visual Studio 2013 and Mvc 5

I am upgrading my site from VS 2010 & MVC 3 to VS 2013 & MVC 5. This works just fine in VS 2010 & MVC 3, where as in the upgrade project (VS 2013 & MVC 5) it is erroring out with the following message.

Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: date

Class

public partial class FileTransferFilterCriteria
    {
        public string Fice { get; set; }
        public string SourceEmail { get; set; }
        public string TargetEmail { get; set; }

        public DateTime? FromDate { get; set; }
        public DateTime? ToDate { get; set; }

        public string Status { get; set; }
        public string Category { get; set; }
    }

Meta Data

[MetadataType(typeof(FileTransferFilterCriteria.FileTransferFilterCriteriaMetaData))]
    public partial class FileTransferFilterCriteria
    {
        public class FileTransferFilterCriteriaMetaData
        {
            [DataType(DataType.DateTime)]
            [Date(ErrorMessage = ValidationMessageConstants.InvalidDate)]
            [UIHint(UiHintEditorTemplateConstants.DateCalendar)]
            [UIHint(UiHintDisplayTemplateConstants.Date)]
            public DateTime? FromDate { get; set; }

            [DataType(DataType.DateTime)]
            [Date(ErrorMessage = ValidationMessageConstants.InvalidDate)]
            [UIHint(UiHintEditorTemplateConstants.DateCalendar)]
            [UIHint(UiHintDisplayTemplateConstants.Date)]
            public DateTime? ToDate { get; set; }
        }

    }

Html

<tr>
                    <th>Upload From Date</th>
                    <td>
                        @Html.EditorFor(x => x.Criteria.FromDate, UiHintEditorTemplateConstants.DateCalendar)
                        @Html.ValidationMessageFor(x => x.Criteria.FromDate)
                    </td>
                </tr>
                <tr>
                    <th>Upload To Date</th>
                    <td>
                        @Html.EditorFor(x => x.Criteria.ToDate, UiHintEditorTemplateConstants.DateCalendar)
                        @Html.ValidationMessageFor(x => x.Criteria.ToDate)
                    </td>
                </tr>

Editor Template

@using System.Globalization

@model DateTime?


@Html.TextBox("", 
(Model != null && 
Model.HasValue && 
!Model.Value.ToString(CultureInfo.InvariantCulture).Contains("1900") &&
!Model.Value.ToString(CultureInfo.InvariantCulture).Contains("0001") 
? 
Model.Value.ToString("MM/dd/yyyy") 
: 
string.Empty), 
new { @class = "datePicker", maxlength = "12", size = "12" })

What is wrong here?

Resulting HTML from VS 2010 & MVC 3 project:

<tr>
                    <th>Upload From Date</th>
                    <td>



<input class="datePicker" data-val="true" data-val-date="Invalid date specified. Date should be like MM/DD/YYYY." id="Criteria_FromDate" maxlength="12" name="Criteria.FromDate" size="12" type="text" value="" />

                        <span class="field-validation-valid" data-valmsg-for="Criteria.FromDate" data-valmsg-replace="true"></span>
                    </td>
                </tr>
                <tr>
                    <th>Upload To Date</th>
                    <td>



<input class="datePicker" data-val="true" data-val-date="Invalid date specified. Date should be like MM/DD/YYYY." id="Criteria_ToDate" maxlength="12" name="Criteria.ToDate" size="12" type="text" value="" />

                        <span class="field-validation-valid" data-valmsg-for="Criteria.ToDate" data-valmsg-replace="true"></span>
                    </td>
                </tr>

Resulting HTML – VS 2013 and MVC 5, when i take out the date attribute:

<tr>
                    <th>Upload From Date</th>
                    <td>


<input class="datePicker" data-val="true" data-val-date="The field Upload From Date must be a date." id="Criteria_FromDate" maxlength="12" name="Criteria.FromDate" size="12" type="text" value="" />

                        <br/><span class="field-validation-valid" data-valmsg-for="Criteria.FromDate" data-valmsg-replace="true"></span>
                    </td>
                </tr>
                <tr>
                    <th>Upload To Date</th>
                    <td>


<input class="datePicker" data-val="true" data-val-date="The field Upload To Date must be a date." id="Criteria_ToDate" maxlength="12" name="Criteria.ToDate" size="12" type="text" value="" />

                        <br/><span class="field-validation-valid" data-valmsg-for="Criteria.ToDate" data-valmsg-replace="true"></span>
                    </td>
                </tr>

Best Answer

Recently I have upgraded MVC3 application to MVC4 application and changed target framework to 4.0. I have no build errors.

But, I got the same error on below line

  @Html.EditorFor(model => model.CmpnySearchReportCriteria.ReportReturnDueDateFrom)

Data annotation attribute was decorated in model as below.

[Display(Name = "Due Date From")]
[DataType(DataType.Date, ErrorMessage = "Please enter a valid date (format: MM/DD/YYYY) for From Report / Return Due Date.")]
public DateTime? ReportReturnDueDateFrom { get; set; }

I have written custom client side validation rule for date input field like below. Because automatic type checking validation for date was not available in MVC3.

public class ModelClientValidationDateRule : ModelClientValidationRule
{
     public ModelClientValidationDateRule(string errorMessage)
     {
           ErrorMessage = errorMessage;
           ValidationType = "date";
      }
}

observed that, MVC4 added automatic type checking validation to any DateTime fields. In MVC4, default client validation message is rendered like below.

data-val-date="The field {PropertyName} must be a date."

Because of our custom client side validation rule data-val-date attribute was rendered twice. This is the cause of this error i.e. "Validation type names in unobtrusive client validation rules must be unique."

<input class="datefield hasDatepicker" **data-val="true" data-val-date="The field UpdateDate must be a date." data-val-date="Please enter a valid date (format: MM/DD/YYYY) for From Date."** id="UpdateDate" name="UpdateDate" type="text" value="">

To Fix this -

  1. Comment data annotation attribute

    public class PreInvoiceSearchCriteria { //[DataType(DataType.Date, ErrorMessage = "Please enter a valid date //(format: MM/DD/YYYY) for From Date.")] public DateTime? RenewDate { get; set; } }

  2. If you don’t want default validation message you can update it using JQuery like below.

$(document).ready(function () {

        $('#DateOfInc').attr("data-val-date", "Please enter a valid date   (format: MM/DD/YYYY) for Date of Inc")
    });

Related Topic