views:

391

answers:

1

I have a DatePicker working on a View which also has a ViewModel associated with it. When I execute the Post action back to the controller the ViewModel is instantiated again and some of the values are not available from the View.

The Controller Action is:

    public ActionResult Search()
    {
        ProjectSearchViewModel viewModel = 
            new ProjectSearchViewModel(
                DateTime.Today.AddMonths(-1), 
                DateTime.Today.AddDays(1));

        return View(viewModel);
    }

  [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Search(ProjectSearchViewModel viewModel)
    {
        try
        {
            //Always returns a value from UI
            DateTime startDate = viewModel.StartDate; 
            //NEVER returns a Value from UI
            DateTime endDate = viewModel.EndDate;

.....

The View markup is:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/TabbedViewMasterPage.master" Inherits="System.Web.Mvc.ViewPage<Reactivity.Web.Models.ProjectSearchViewModel>" %>

...

 <label for="StartDate">
        Start Date:
    </label>
    <% Html.jQuery().DatePicker()
            .Name("StartDate")
            .AllowMonthChange(true)
            .AllowYearChange(true)
            .ShowOn(DatePickerShowOn.Focus)
            .ShowOtherMonths(true)
            .Value(ViewData.Model.StartDate)
            .Render(); %>
    <br />
    <label for="EndDate">
        End Date:
    </label>
    <% Html.jQuery().DatePicker()
            .Name("EndDate")
            .AllowMonthChange(true)
            .AllowYearChange(true)
            .ShowOn(DatePickerShowOn.Focus)
            .ShowOtherMonths(true)
            .Value(ViewData.Model.EndDate)
            .Render(); %>
    <br />
    <input type="submit" value="Search" />

The DatePicker (two instances on View) work fine.

How do I ensure the ViewModel fields (viewModel.EndDate) are returned populated to the Controller Action? Or is this an issue with having two (MVC) DatePickers on the form?

Many thanks Brian

A: 

Ok, there are two issues here:

  1. I´m new to MVC etc., and had put the Telerik MVC Extensions in to my project which to you jQuery guys will know that the code above is Telerik CTP. Sorry about that.

  2. The issue in the end was rather interesting. The code above was not handling date localisation correctly such that dates that were entered in the datepicker with browser localisation set to NON-US English were being handled as US format such that when I selected 28/07/2009 (a correct en-UK format) either Telerik wrapper for the datepicker control would not pass it throught or something in the pipeline was rejecting it. The result was that any dates beyond the 12th of the month were not being returned.

I hope this helps someone else along the way.

Brian

Redeemed1