将选定的DropDown值传递给控制器

时间:2015-04-27 07:00:08

标签: asp.net-mvc asp.net-mvc-5

我的View上有一个DropDownList和一个按钮。单击一个按钮,我想将选定的DropDown值传递给控制器​​。怎么做?

查看代码如下:

@model Sample2.Models.LeaveType
@{
    ViewBag.Title = "Status of Application";
}

@{
    List<SelectListItem> listItems = new List<SelectListItem>();
    listItems.Add(new SelectListItem
         {
             Text = "All",
             Value = "A",
             Selected = true
         });
    listItems.Add(new SelectListItem
         {
             Text = "Recommended",
             Value = "R"
         });
    listItems.Add(new SelectListItem
         {
             Text = "Sanctioned",
             Value = "S"
         });
}

<table>
    <tr>
        <td>
            @Html.DropDownListFor(model => model.LeaveType1, listItems, "-- Select Status --")
        </td>
        <td>
            @using (Html.BeginForm("Index", "Home", FormMethod.Get))
            {
                <input type="submit" value="Show" />
            }
        </td>
    </tr>
</table>

控制器代码如下:

    public ActionResult Index(string Id)
    {
        using (var context = new Admin_TestEntities())
        {
            var data = context.Leave_GetDetails(Id).ToList();
            return View(data.AsEnumerable().ToList());
        }

    }

**编辑**

建议之后,我更改了下面的代码,它正在运行:

        @using (Html.BeginForm("Index", "Home", FormMethod.Post))
        {
            @Html.DropDownList("Id", listItems)
            <input type="submit" value="Show" />
        }

1 个答案:

答案 0 :(得分:2)

你可以这样做:

[Post] public ActionResult Index(string fromList) { ... }

视图可能如下所示:

<div>
@Html.BeginForm("Index", "Home", FormMethod.Post)
{
     @Html.DropDownListFor(model => model.LeaveType1, listItems, "-- Select Status --")
     <input type="submit" />
}
</div>