0

I am trying to bind the selected date from the jQueryUI datepicker to the model to be used in the controller, but its returning null. I am able to retrieve the selected value when formcollection is used

ViewModel

public class EmpViewModel
 {
    public int EmpId { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public DateTime DateOfBirth { get; set; }

}

View

<div class="col-md-10">
    @Html.EditorFor(model => model.DateOfBirth, 
            new { htmlAttributes = new { @class = "form-control datefield" } })
</div>

Controller

public ActionResult Create(EmpViewModel emp)
{
}

Tried the method in the below article but couldn't get it working http://www.asp.net/mvc/overview/older-versions/using-the-html5-and-jquery-ui-datepicker-popup-calendar-with-aspnet-mvc/using-the-html5-and-jquery-ui-datepicker-popup-calendar-with-aspnet-mvc-part-4

Script

<script>
    $(function () {
        $(".datefield").datepicker();
    })
</script>
Sunil Kumar
  • 3,142
  • 1
  • 19
  • 33
user3543878
  • 167
  • 1
  • 2
  • 11

1 Answers1

0

There are multiple solutions to bind datepicker into viewmodel, but these two are common ones:

First, try declaring an id attribute on EditorFor and bind jQuery datepicker to it.

@Html.EditorFor(model => model.DateOfBirth, 
            new { htmlAttributes = new { @id = "DateOfBirth", @class = "form-control datefield" } })

<script>
    $(function () {
        $("#DateOfBirth").datepicker();
    });
</script>

Second, try HttpPost attribute on your controller method if your model passed to controller using form submission, and use ModelState.IsValid when necessary.

[HttpPost]
public ActionResult Create(EmpViewModel emp)
{
    if (ModelState.IsValid)
    {
        // do something
    }
}

If those solutions still won't work, change EditorFor to TextBoxFor and check how the submission works using both form collection parameters and model binding scheme.

Additional things to concern with:

  1. Ensure your EditorFor enclosed inside <form> tag, or try @using (Html.BeginForm("Create", "Controller", FormMethod.Post)) and place the EditorFor inside it.

  2. Ensure the date format supplied by datepicker is in correct format recognizable by System.DateTime, don't rely on JS format (avoid using toLocaleDateString if any). Use momentjs library if you want to use specific date format.

  3. Ensure the jQuery is loaded properly, check your browser console for jQuery's presence after page load (see if TypeError: jQuery is not defined message exists).

  4. Try setting the plain DateTime as nullable DateTime: public DateTime? DateOfBirth { get; set; } and insert some code to check against null value.

Similar problems as further reference:

MVC 4 DatePicker's selected value not getting set to model

Bind Datetime Model value to datepicker and pass them to controller method

Community
  • 1
  • 1
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • Implementing first point by adding id is binding the incorrect date of 01/01/0001 12:00:00 AM, how do i "Ensure the date format supplied by datepicker is in correct format recognizable by System.DateTime" do this ? – user3543878 Sep 22 '16 at 04:26
  • Try to use `console.log($("#DateOfBirth").val())` or similar way in client side to get the datetime output value given by `EditorFor`. You can check if the console output is suitable as proper `DateTime` format when passed to the model. – Tetsuya Yamamoto Sep 22 '16 at 04:34
  • Stephen's solution worked for me in this http://stackoverflow.com/questions/28042366/how-to-bind-a-textbox-to-a-datetime-field-but-display-only-its-date-part-in-asp?rq=1 – user3543878 Sep 26 '16 at 06:37