如何在aspx引擎中获取Html.DropDownListFor的选定值?

时间:2012-08-19 04:51:34

标签: asp.net asp.net-mvc-3 html.dropdownlistfor

在Html中(我没有使用)它是

<select>
<option value="B">Bubblegum</option>
</select>

In(来自控制器)Aspx它是

[HttpGet]
public ActionResult ViewMethod()
{
//Populate DropDownList
Treats = new List<KeyValuePair<string, string>>();
Treats.Add(new KeyValuePair<string, string>("Bubblegum", "B"));
ViewData["treats"] = new SelectList(Treats, "Value", "Key", SelectedTreat);
return View();
}
[HttpPost]
public ActionResult GeneratedContent(SomeModel model)
{
List<HalloweenTreats> ChewingGums = StoredProc(model.SelectedTreat);
return PartialView(ChewingGums);
}

然后在视图中:

<% using (Ajax.BeginForm("ViewMethod", "ControllerName", new AjaxOptions { UpdateTargetId = "DisplayValues" })) { %>

<%: Html.DropDownListFor(x => x.SelectedTreat, ViewData["treats"] as SelectList)%>
<input type = 'submit' name = 'submit' value = 'generate' />
<div id="DisplayValues"></div>
<% } %>
<%: Ajax.ActionLink("Generate", "RequestReportSummary", "Report", new { model = Model }, new AjaxOptions { UpdateTargetId = "GeneratedReport" })%>

如何在aspx引擎中获取Html.DropDownListFor的选定值“B”,以便将其作为字符串发送到数据库中?

1 个答案:

答案 0 :(得分:1)

您要发布的控制器操作可以将您的模型作为参数:

[HttpPost]
public ActionResult ViewMethod(SomeModel model)
{
    // model.SelectedTreat will contain the selected value here
}