user3035874
user3035874

Reputation:

Always getting "The field Date must be a date" validation message

I'm always getting the validation message like following when I submit the form to call post Action (Method)

The field ReportStartDate must be a date.

My model is

public class ReportModel
{
    public DateTime? ReportStartDate { get; set; }
    public DateTime? ReportEndDate { get; set; }
}

My view is like the following,

@using(@Html.BeginForm())
{
    <div>
        @Html.Bootstrap().ControlGroup().TextBoxFor(m => m.ReportStartDate).Label().LabelText("Start Date")
        @Html.Bootstrap().ControlGroup().TextBoxFor(m => m.ReportEndDate).Label().LabelText("End Date")

        <div class="form-actions align-right">
            @Html.Bootstrap().SubmitButton().Text(AOCrowdFund.Resources.Global.Report).Id("GenerateReport")
        </div>
    </div>
}

Note:
I am using jquery datepicker for this fields

Note:

Even if i use like the following, I'm getting the same error..

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    [Required(ErrorMessageResourceName = "ErrFieldRequired", ErrorMessageResourceType = typeof(Resources.Global))]
    public DateTime? ReportStartDate { get; set; }


    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    [Required(ErrorMessageResourceName = "ErrFieldRequired", ErrorMessageResourceType = typeof(Resources.Global))]
    public DateTime? ReportEndDate { get; set; }

Upvotes: 2

Views: 5724

Answers (3)

Jeeva J
Jeeva J

Reputation: 3253

I can't say the exact one about what to do. But you can do by the following. It'll help you out temporarily as you expected.

Add below jquery code for yours

$('#DatePicker').removeAttr("data-val-date");

After that validate your date in controller and add model error..

ModelState.AddModelError(String.Empty,"Invalid date");

I hope this will help you out temporarily.

Upvotes: 2

Dan
Dan

Reputation: 15

Try specifying the altField and altFormat in the JQuery datepicker

$("#form").datepicker({
  altField: "#form",
  altFormat: "yy-mm-dd"
});

Upvotes: 0

Sakthivel
Sakthivel

Reputation: 1938

What is the value of ReportStartDate and ReportEndDate ? Do you use jquery datepicker or something ?

Ususally, this error occurs if the format of input is not a valid date format. and also check if it works in other browsers(happens on some browser updates.)

similar question Here

Upvotes: 0

Related Questions