如何从MVC的DropDown中获取选定的值?我想将其分配给一个变量。

这是我的 Controller 操作:

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;
}

这是我的看法:

@Html.DropDownList("Options", ViewData["Options"] as SelectList, "--Select Item--")

最佳答案

查看

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

    <input type="submit" name="submit" value="Submit" />
}

Controller
新增 Action
[HttpPost]
public ActionResult Drop(FormCollection form)
{
    var optionsValue = form["Options"];
    //TODO:
    return RedirectToAction("Drop");
}

关于c# - 如何在MVC中获取DropDownList的选定值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12050143/

10-12 12:36