我无法在 mvc3 中获取下拉列表值



我有一个下拉列表元素的表单。 我想在控制器中获取下拉列表的值。我不知道怎么能做到这一点。我在控制器中使用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");
    }

你试过吗:

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)

最新更新