MVC Razor gets option value from select using FormCollection

My view has a choice 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.

+7
html select asp.net-mvc razor formcollection
source share
2 answers

Try this to get control value in formcollection

 formCollection["inputRole"] 

Your code will become

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

You can simply access your form field by its name this way

  String role = formCollection["inputRole"]; 
+5
source share

All Articles