0

在我看来,我有这段代码:

 <% 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);
  }

我收到一条错误消息,指出传递的参数 date 为 null 我哪里出错了?

4

1 回答 1

0

ModelBinding 不适用于名为的参数date,因为它与您的选择字段的名称不同。在您的操作参数中使用正确的名称。

public ActionResult TradeUKKPIShowData(DateTime SelectedSunday) {}

您还可以绑定您的 ViewModel(不知道类型,所以我使用TypeOfYourViewModel

public ActionResult TradeUKKPIShowData(TypeOfYourViewModel model) 
{
     var reportData = _reportingService.GetTradeUKKPISearches(model.SelectedSunday);
     ViewBag.reportdate = date;
     return View(reportData);
}
于 2012-08-15T13:49:12.373 回答