通过Html.BeginForm将下拉列表中的选定值传递回控制器

时间:2012-08-15 13:44:21

标签: asp.net-mvc html-helper

我的观点中有这个代码:

 <% 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 哪里出错了?

1 个答案:

答案 0 :(得分: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);
}
相关问题