2009-08-28 89 views
1

我有一個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的問題?

非常感謝 布賴恩

回答

1

好了,這裏有兩個問題:

  1. I'm新MVC等,已經把Telerik的MVC擴展在我的項目,它給你jQuery的人會知道上面的代碼是Telerik CTP。對於那個很抱歉。

  2. 這個問題最後很有意思。上面的代碼沒有正確處理日期本地化,因此在瀏覽器本地化設置爲非美國英語的日期選擇器中輸入的日期被作爲美國格式處理,因此當我選擇28/07/2009(正確的英國英國格式)日期選擇器控件的Telerik包裝器不會傳遞它,或者管道中的某個東西拒絕它。結果是,任何超出本月12日的日期都沒有被退回。

我希望這可以幫助別人。

Brian