0

I am using the below Ajax form to select and post date ranges.

View Model:

public class ViewFormSubmissionsVM
    {
        public string FormName { get; set; }
        public Guid FormId { get; set; }

        [Display(Name="From:")]
        public DateTime From { get; set; }

        [Display(Name = "To:")]
        public DateTime To { get; set; }
    }

Razor/HTML:

@model ViewFormSubmissionsVM

    @using (Ajax.BeginForm("ViewFormSubmissions", "Form", new AjaxOptions { HttpMethod = "POST", OnSuccess = "onGetSubmissionSuccess", OnFailure = "onGetSubmissionError" }, new { @class = "form-horizontal" }))
    {
        @Html.AntiForgeryToken()
        @Html.HiddenFor(x => x.FormId)
        <div class="row col-md-12">
            <div class="pull-right">
                <label class="control-label col-md-3">@Html.DisplayNameFor(x => x.From)</label>
                @Html.TextBoxFor(x => x.From, "{0:MM/dd/yyyy}", new { @class = "form-control col-md-3", @data_picker = "date-picker" })
                <label class="col-md-3">@Html.DisplayNameFor(x => x.To)</label>
                @Html.TextBoxFor(x => x.To, "{0:MM/dd/yyyy}", new { @class = "form-control col-md-3", @data_picker = "date-picker" })
                <input type="submit" class="btn btn-primary" value="Search" />
            </div>
        </div>
    }

JQuery Date picker:

$(document).ready(function () {
    $('*[data-picker="date-picker"]').datepicker();
});

This issue is while submitting the form, the "From" and "To" Datetime properties, received the default date values instead of selected.

enter image description here

Am I missing anything important!! I have already used these codes in different forms, but never experienced this before.

Could any one help me to get out of this one.

Many Thanks.

Gopinath
  • 246
  • 1
  • 4
  • 16
  • Are the model dates blank or have you set values to them? – Karl Gjertsen Jan 28 '16 at 13:56
  • 1
    It has values, return View(new ViewFormSubmissionsVM { FormId = id, From = DateTime.Now.AddMonths(-1), To = DateTime.Now }); – Gopinath Jan 28 '16 at 13:58
  • What is the culture on the server? Is it invariant or one that accepts `DateTime` in the format `MM/dd/yyyy`? –  Jan 28 '16 at 22:44

1 Answers1

1

First of all, I think you should call this overload:

using (Ajax.BeginForm("ViewFormSubmissions", "Form", null, new AjaxOptions{ 
    HttpMethod = "POST", 
    OnSuccess = "onGetSubmissionSuccess", 
    OnFailure = "onGetSubmissionError" 
    }, 
    new {@class = "form-horizontal" }))

with null as 3rd parameter.

Second, check your datetime format. You can find more info here: MVC DateTime binding with incorrect date format, ASP.NET MVC datetime culture issue when passing value back to controller or http://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization.

Community
  • 1
  • 1
romanoza
  • 4,775
  • 3
  • 27
  • 44
  • Sorry, there is no luck with this.:( – Gopinath Jan 29 '16 at 12:45
  • 1
    @user3534886 Try to change the type of `To` and `From` from `DateTime` to `string` and see what is passed to the controller. – romanoza Jan 29 '16 at 12:46
  • Yes. Using "string" type instead of "DateTime" working fine. But what is the reason behind this? – Gopinath Jan 29 '16 at 14:36
  • 1
    @user3534886 What is the DateTime format you are getting in the `To` / `From` fields? What is the DateTime format expecting by the server? You can check this out: http://stackoverflow.com/questions/7889038/asp-net-mvc-datetime-culture-issue-when-passing-value-back-to-controller/7889455#7889455 and http://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization. – romanoza Jan 29 '16 at 15:42