MVC Razor get option value from select with FormCollection

Lord Vermillion picture Lord Vermillion · Jan 23, 2014 · Viewed 44.8k times · Source

My view has a Select with elements(options) from my ViewModel.

        @using (Html.BeginForm("NewUser", "Admin"))
        {
             <select multiple="" id="inputRole" class="form-control" size="6" name="inputRole">
             @foreach (var item in Model.roller)
             {
                 <option>@item.Name</option>
             }
             </select>
         }

How can i get the selected value in my Controller?

    [HttpPost]
    public ActionResult NewUser(FormCollection formCollection)
    {
        String roleValue1 = formCollection.Get("inputRole");
    }

This gives me a null value.

Answer

Nitin Varpe picture Nitin Varpe · Jan 23, 2014

Try this to get the value of control in the formcollection

formCollection["inputRole"]

Your code becomes

[HttpPost]
    public ActionResult NewUser(FormCollection formCollection)
    {
        String roleValue1 = formCollection["inputRole"];
    }