如何在MVC中获取DropDownList的选定值

bei*_*fab 6 c# asp.net-mvc

如何从MVC中的DropDown中获取所选值?我想将它分配给变量.

这是我的控制器动作:

public ActionResult Drop()
{
    List<SelectListItem> items = new List<SelectListItem>();
    items.Add(new SelectListItem { Text = "Action", Value = "0" });
    items.Add(new SelectListItem { Text = "Drama", Value = "1" });
    items.Add(new SelectListItem { Text = "Comedy", Value = "2" });
    items.Add(new SelectListItem { Text = "Science Fiction", Value = "3" });
    items.Add(new SelectListItem { Text = "Horror", Value = "4" });
    items.Add(new SelectListItem { Text = "Art", Value = "5"  });
    ViewData["Options"] = items;
}
Run Code Online (Sandbox Code Playgroud)

这是我的看法:

@Html.DropDownList("Options", ViewData["Options"] as SelectList, "--Select Item--")
Run Code Online (Sandbox Code Playgroud)

Cha*_*lie 4

看法

@using (Html.BeginForm())
{ 
    <h2>Drop</h2>
    @Html.DropDownList("Options", ViewData["Options"] as SelectList, "--Select Item--")

    <input type="submit" name="submit" value="Submit" />
}
Run Code Online (Sandbox Code Playgroud)

控制器 添加新操作

[HttpPost]
public ActionResult Drop(FormCollection form)
{
    var optionsValue = form["Options"];
    //TODO:
    return RedirectToAction("Drop");
}
Run Code Online (Sandbox Code Playgroud)