Jap*_*TIA 2 asp.net-mvc selecteditem html.dropdownlistfor razor asp.net-mvc-4
我知道如何获取DropDownList的选定值有多个线程.但是我找不到从控制器的局部视图中获取此值的正确方法.
这是我的部分观点:
@model List<aptest.Models.answer>
@Html.DropDownList("dropdownlist", new SelectList(Model, "text", "text"))
<button type="submit">next</button>
Run Code Online (Sandbox Code Playgroud)
And*_*rei 10
要获取下拉值,请将选择列表包装在表单标记中.使用模型和DropDownListFor
帮助器
@model MyModel
@using (Html.BeginForm("MyController", "MyAction", FormMethod.Post)
{
@Html.DropDownListFor(m => m.Gender, MyModel.GetGenderValues())
<input type="submit" value="Send" />
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
如果您使用局部视图,只需将模型传递给它:
@Html.Partial("MyPartialView", Model)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
46893 次 |
最近记录: |