7

MVC のドロップダウンから選択した値を取得するにはどうすればよいですか? 変数に代入したい。

これは私のコントローラーアクションです:

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--")
4

3 に答える 3

4

意見

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

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

コントローラ 新しいアクションを追加

[HttpPost]
public ActionResult Drop(FormCollection form)
{
    var optionsValue = form["Options"];
    //TODO:
    return RedirectToAction("Drop");
}
于 2012-08-21T07:42:13.757 に答える
3

また、投稿で FormCollection を使用していない場合は、次を簡単に使用できることを考慮してください。これは、特にメイン ビュー内で部分ビューを使用している場合に非常に役立ちます。

[HttpPost]
public ActionResult Drop(SelectListItem item)
{

    var selectedValue = Request.Form["ID_OF_THE_DROPDOWNLIST"];
    //TODO......
    return RedirectToAction("Drop");
}
于 2013-04-24T18:37:47.310 に答える
0

意見 :

@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
    <fieldset>
        Select filter
        @Html.DropDownList("SelectFilter", @Model.ddlList)
        <p>
            <input type="submit" value="Submit" />
        </p>
    </fieldset>
}

コントローラー:

        public ActionResult Index(string SelectFilter)
        {
            var _model = new Models.MyModel();

            List<SelectListItem> listDDL = new List<SelectListItem>();
            listDDL.Add(new SelectListItem { Text = "11", Value = "11" });
            listDDL.Add(new SelectListItem { Text = "22", Value = "22" });
            listDDL.Add(new SelectListItem { Text = "33", Value = "33" });
            ViewData["ddlList"] = listDDL;

//We add our DDL items to our model, you can add it to viewbag also
//or you can declare DDL in view with its items too
            _model.ddlList = listDDL;
            return View();
        }

モデル :

Public class MyModel
{
     public List<SelectListItem> ddlList = new List<SelectListItem>();

}
于 2015-03-01T15:57:29.193 に答える