MVC剃刀从选择具有的FormCollection获得期权价值 [英] MVC Razor get option value from select with FormCollection

查看:136
本文介绍了MVC剃刀从选择具有的FormCollection获得期权价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的看法与元素(选择)一个选择从我的ViewModel。

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");
    }

这给了我一个空值。

推荐答案

试试这个让在的FormCollection控件的值

Try this to get the value of control in the formcollection

formCollection["inputRole"]

您code变为

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

这篇关于MVC剃刀从选择具有的FormCollection获得期权价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆