Search code examples
asp.net-mvcjquery-uijquery-ui-datepicker

Pass JQuery datepicker selected value as MVC ViewModel property to Controller Action


I am wondering is it possible to do this: The view has a ViewModel bind to it. When submit the form as a POST to Controller action for all the input values from the form, get the date picked from JQuery datepicker input, set as one of the ViewModel's property?

e.g. The ViewModel has vm.InputDate as a property for this datepicker field, instead having a seperated parameter in Controller's action method, can the date input set as the ViewModel's InputDate property's value and pass to Controller?

So instead of having

public ActionResult Submit(ViewModel vm, DateTime inputDate)

I would like to have

public ActionResult Submit(ViewModel vm)

but vm.InputDate is set to inputDate from JQuery datepicker selection


Solution

  • I figured out by doing: In the View:

    @Html.EditorFor(model => model.InputDate, new { htmlAttributes = new { @id = "inputDatePicker", @class = "datepicker", @Name = "inputDate", @PlaceHolder = "mm/dd/yyyy" } })
    
    $(function () {
            $("#inputDatePicker").datepicker(
                {
                    showOtherMonths: true,
                    selectOtherMonths: true
                });
        });
    

    In the Controller:

    public ActionResult Replace(ViewModel vm)