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