2016-12-23 16 views
2

我有複選框選項,我檢查它後,我改變了輸入字段的值,只顯示一個月和年。在未選中複選框的情況下。用戶點擊輸入,日期選擇器提供選擇日期(月/年)的選項。第一個選項的值不會正確發佈,第二個選項是肯定的。我做錯了什麼?用jquery設置一個值,當提交數據時刪除綁定到ViewModel

enter image description here

取消選擇將今年的日期和點擊輸入,而不是

enter image description here

現在,如果我檢查(年初至今),併發布值,那麼的startDate年底結束日期有一個空值。

我的控制器是遵循

[HttpPost, ValidateAntiForgeryToken] 
    public ActionResult Index(ManualRecViewModel vm) 

我的視圖模型具有領域的startDate和結束日期

public class ManualRecViewModel 
{ 
     public DateTime? startDate { get; set; } 
     public DateTime? endDate { get; set; } 
} 

和我的看法是

<div class="input-group"> 
     <div> 
      @Html.CheckBox("year-to-date", false) 
      @Html.Label("Year to Date") 
     </div> 

     <div class="dates-filters"> 
      <div> 
       @Html.Label("From:") 
      </div> 
      <div> 
       @Html.TextBoxFor(m => m.startDate, new { @readonly = "readonly" }) 
      </div> 
      <div> 
       @Html.Label("To:") 
      </div> 
      <div> 
       @Html.TextBoxFor(m => m.endDate, new { @readonly = "readonly" }) 
      </div> 
     </div> 
    </div> 

我有一個設置的值,以一個js輸入

var date = new Date(); 
    var year = date.getFullYear(); 
    var month = date.getMonth(); 
    $("#startDate").datepicker('setDate', new Date(year, 0, 1)); 
    $("#endDate").datepicker('setDate', new Date(year, month, 1)); 
+0

您是否嘗試過檢查傳遞給你的控制器的值,使用JS?您可能會發現最終日期值無效,並且會發生模型綁定問題。 – G0dsquad

+0

我不是用js發佈這些值,輸入是在一個帶有提交按鈕的表單裏面 – Heinrich

+0

什麼是'CultureInfo'? – Shyju

回答

0

我有這個問題之前,發現使用$.trigger("change")方法是必要的,因爲使用設置和$.val()方法實際上並不導致輸入字段的值更改表單提交

+0

實際上,我的功能是針對複選框$(「#year-to-date」)的change事件。change(yearToDate);但我怎樣才能做到這一點? – Heinrich

+0

我在說的是在執行'$ .setDate()'調用之後,您還應該調用'$(「#startDate,#endDate」)。trigger(「change」)'來獲取日期輸入來完成更改 – Bwolfing

+0

其他選項將在您的C#服務器代碼中,檢查用戶是否選擇YTD複選框,以及他們是否忽略提交的日期,然後確定服務器上的YTD – Bwolfing

相關問題