我知道如何获取DropDownList的选定值有多个线程。但是我找不到从控制器的局部视图中获取此值的正确方法。
这是我的部分观点:
@model List<aptest.Models.answer>
@Html.DropDownList("dropdownlist", new SelectList(Model, "text", "text"))
<button type="submit">next</button>
答案 0 :(得分:10)
要获取下拉值,请将选择列表包装在表单标记中。使用模型和DropDownListFor
助手
@model MyModel
@using (Html.BeginForm("MyController", "MyAction", FormMethod.Post)
{
@Html.DropDownListFor(m => m.Gender, MyModel.GetGenderValues())
<input type="submit" value="Send" />
}
public class MyController : Controller
{
[HttpPost]
public ActionResult MyAction(MyModel model)
{
// Do something
return View();
}
}
public class MyModel
{
public Gender Gender { get; set; }
public static List<SelectListItem> GetGenderValues()
{
return new List<SelectListItem>
{
new SelectListItem { Text = "Male", Value = "Male" };
new SelectListItem { Text = "Female", Value = "Female" };
};
}
}
public enum Gender
{
Male, Female
}
如果您使用局部视图,只需将模型传递给它:
@Html.Partial("MyPartialView", Model)
答案 1 :(得分:1)
ViewData["list"] = myList.ToList();
剃刀
@Html.DropDownList("ddl", new SelectList((System.Collections.IEnumerable)ViewData["list"], "Id", "Name"))
<强>控制器强>
public ActionResult ActionName(String ddl)
{
// now ddl has your dropdownlist's selected value i.e Id
}