我有一個DatePicker工作在一個視圖,它也有一個ViewModel與它關聯。 當我將Post操作執行回控制器時,ViewModel會再次實例化,並且某些值不可用於View。ASP.Net MVC和jQuery DatePicker ViewModel
控制器中的操作是:
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;
.....
觀的標記是:
<%@ 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" />
DatePicker(View上的兩個實例)工作正常。
如何確保將ViewModel字段(viewModel.EndDate)返回填充到Controller Action?或者這是表單上有兩個(MVC)DatePickers的問題?
非常感謝 布賴恩