我知道如何获取DropDownList的选定值有多个线程.但是我找不到从我的控制器中的局部视图中获取此值的正确方法.
这是我的部分观点:
- @model List<aptest.Models.answer>
- @Html.DropDownList("dropdownlist",new SelectList(Model,"text","text"))
- <button type="submit">next</button>
解决方法
要获取下拉值,请将选择列表包装在表单标记中.使用模型和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)