Pass selected DropDown value to the controller

2019-05-24 13:27发布

I have a DropDownList and a button on my View. On click of a button, I want to pass the selected DropDown value to the controller. How to do this?

View code is as below:

@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>

Controller code is like this:

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

    }

** Edited **

After suggestions, I changed the code as below and it is working:

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

1条回答
Luminary・发光体
2楼-- · 2019-05-24 13:48

you can do it like that:

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

the view can look something like this:

<div>
@Html.BeginForm("Index", "Home", FormMethod.Post)
{
     @Html.DropDownListFor(model => model.LeaveType1, listItems, "-- Select Status --")
     <input type="submit" />
}
</div>
查看更多
登录 后发表回答