我查看了所有相关问题,但我仍然不确定。我是一个菜鸟,还在学习。
我有观点:
<% using (Html.BeginForm("TradeUKKPISearchesDataExtract", "Report")) //action/controller
{ %>
<div style="padding: 10px; background-color: #eeeeee; width: 250px; border: 1px solid #cccccc;">
<div style="float: left; width: 100px; padding-top: 3px;">
<p style="color:Red"><%: Html.DropDownList("Choose a Sunday: ", ((DateTime[])ViewData["Sundays"]).Select(day => new SelectListItem() { Text = day.ToString(), Value = day.ToString() }), "--Select--")%>
</p>
</div>
<div class="clear"></div>
</div>
<div style="text-align:right; width: 272px; padding-top: 30px;">
<input type="submit" id="search-submit" value="Search" />
</div>
当我点击提交按钮时,我在哪里设置需要返回的值?
在html.BeginForm或Html.DropDownList?
中我的行动:
[HttpPost]
public ActionResult TradeUKKPISearchesDataExtract(DateTime date)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Title, Total");
var reportData = _reportingService.GetTradeUKKPISearches(date);
foreach (var item in reportData)
{
sb.AppendLine(String.Concat("\"", item.Key, "\", ", item.Value));
}
byte[] textBytes = Encoding.UTF8.GetBytes(sb.ToString());
return File(textBytes, "application/csv", "TradeUKKPISearchesDataExport.csv");
}
感谢
答案 0 :(得分:1)
Html.DropDownList()
的第一个参数是HTML元素的名称,它是在表单提交期间识别所选值的方式。所以你应该像这样
<p style="color:Red">Choose a Sunday:<%: Html.DropDownList("date", ((DateTime[])ViewData["Sundays"]).Select(day => new SelectListItem() { Text = day.ToString(), Value = day.ToString() }), "--Select--")%>
正确填充操作方法中的date
参数。