我无法在mvc3中获得dropdownlist值

时间:2014-02-10 16:06:48

标签: visual-studio-2012 model-view-controller

我有一个dropdownlist元素的表单。我想在控制器中获得dropdownlist的值。 我不知道怎么能这样做。我在控制器中读取了带有dataview的用户用户名,并希望通过下拉列表选项更改它们的角色。

@using (Html.BeginForm()) {
<form name="register" action="#"> 
<div>

  <table>

      @foreach (MembershipUser user in Model)
        {
            var userroles = Roles.GetRolesForUser(user.UserName);
              <tr>
                  <td>
                      @user.UserName
                  </td>
                  @foreach (var role in userroles)
                     {
                          <td>
                              @role
                          </td>
                    }

                  <td>
                       @Html.DropDownList("roleName")
                       @Html.ValidationMessage("roleName")

                  </td>
                  <td>
                  </td>
              </tr>
      }       

  </table>
<input type="submit" class="register" value="Save" />
</div>

    </form>

   }

这是我的控制者:

  public ActionResult Index()
    {
        ViewData["roleName"] = new SelectList(Roles.GetAllRoles(), "roleName");
        return View(Membership.GetAllUsers()); 
    }
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index()
    {
        return RedirectToAction("Index", "Home");

    }

1 个答案:

答案 0 :(得分:0)

你有没有尝试过:

public ActionResult Index(string roleNameSelectedValue)
{
    // roleNameSelectedValue is the "Value" field of selected item in your dropdownlist
    return RedirectToAction("Index", "Home");
}

或使用@Html.DropDownListFor(m => Model.roleName, ViewBag.roleName as SelectList)