Usama Shakeel
Usama Shakeel

Reputation: 39

datetimepicker giving null value to ActionResult when greater than 12 in mvc 5

I have startdate and enddate column when I select date less than or equal to 12 it work fine but when i select date greater than 12 it give null value to ActionResult.

this is the function i am using.

$(function () {
    $('body').on("focus", ".datepicker", function () {
        $('.datepicker').datetimepicker({
            format: 'MM-DD-YYYY'
        })
    })
});

Text Box From view from which i am selecting date.

@Html.TextBoxFor(model => model.StartDate, new { placeholder = "MM-dd-yyyy", @class = "form-control datepicker" })
@Html.ValidationMessageFor(model => model.StartDate)

and Validation of this text box.

[Required(ErrorMessage = "Start Date is required."), DataType(DataType.Date)]
public Nullable<System.DateTime> StartDate { get; set; }
[Required(ErrorMessage = "End Date is required."), DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public Nullable<System.DateTime> EndDate { get; set; }

Upvotes: 2

Views: 1189

Answers (2)

rased
rased

Reputation: 136

Inside <system.web></system.web> in your web.config file, add this line <globalization culture="en-GB" uiCulture="en-GB" />

Set culture en-GB for dd/MM/yyyy and en-US for mm/dd/yyyy. Hope this helps.

Upvotes: 2

GGO
GGO

Reputation: 2748

In the datetimepicker declaration you precise a format MM-DD-YYYY (month-day-year) and on the EndDate property you are using DataFormatString = "{0:dd/MM/yyyy}" day/month/year.

Homogenize all your dates formats, using the same order and the same separator, and add a DataFormatString decorator on StartDate too

Upvotes: 1

Related Questions