2012-08-15 47 views
0

我有這樣的代碼在我看來:通行證通過Html.BeginForm

<% using (Html.BeginForm("TradeUKKPIShowData", "Report")) //action/controller 
{ %> 
    <div style="padding: 10px; background-color: #eeeeee; width: 300px; border: 1px solid #cccccc;"> 
     <div style="float: left; width: 150px; padding-top: 3px;"> 
     <%: Html.Label("Select a Sunday:") %> 
     </div> 
     <div style="float: left; width: 150px;"> 
     <%: Html.DropDownListFor(model => model.SelectedSunday, Model.AllSundays) %> 
     </div> 
    <div class="clear"></div> 
    </div> 
    <div style="text-align:right; width: 272px; padding-top: 30px;"> 
    <input type="submit" id="search-submit" value="Submit"/> 
    </div> 
<% } %> 

控制器中的動作:

public ActionResult TradeUKKPIShowData(DateTime date) //this date value is null thus error 
    { 
    var reportData = _reportingService.GetTradeUKKPISearches(date); 
    ViewBag.reportdate = date; 
    return View(reportData); 
    } 

我得到一個錯誤,指出參數傳遞,日期,爲空 我在哪裏出錯了?

回答

0

ModelBinding不適用於名稱爲date的參數,因爲它與您所選字段的名稱不同。在您的操作參數中使用正確的名稱。

public ActionResult TradeUKKPIShowData(DateTime SelectedSunday) {} 

你也可以綁定您的視圖模型(不知道是什麼類型,所以我使用TypeOfYourViewModel

public ActionResult TradeUKKPIShowData(TypeOfYourViewModel model) 
{ 
    var reportData = _reportingService.GetTradeUKKPISearches(model.SelectedSunday); 
    ViewBag.reportdate = date; 
    return View(reportData); 
}