更改时通过DropDownList传递多个数据字段

时间:2019-04-25 19:58:07

标签: c# asp.net-mvc visual-studio-2017

我当前正在尝试创建一个“修改用户角色”页面。以下是到目前为止我已经完成的工作...

Example

(用户名字段确实填充,我现在暂时将其清空。)

下面是我的视图下拉列表...

@Html.DropDownList("Roles", new SelectList(ViewBag.RoleList), "Modify Role", new { @class = "form-control" })

这是我需要它调用的控制器动作

    [AuthLog(Roles = "Super Admin")]
    [HttpPost]
    public ActionResult ModifyUser(string id)
    {
        string newRole = Request.Form["Roles"];
        if (id != null && newRole != null)
        { 
            ApplicationDbContext context = new ApplicationDbContext();
            var newRoleName = context.Roles.FirstOrDefault(u => u.Name == newRole);
            var newid = newRoleName.Id;
            context.UpdateRole(id, newid);
        }
        return RedirectToAction("Index", "UserControl");
    }

以下是我要发送给方法的数据,以及如何创建下拉列表。

    {
        [AuthLog(Roles = "Super Admin")]
        public ActionResult Index()
        {
            ApplicationDbContext context = new ApplicationDbContext();
            var usersWithRoles = (from user in context.Users
                                  select new
                                  {
                                      UserId = user.Id,
                                      Username = user.UserName,
                                      user.Email,
                                      RoleNames = (from userRole in user.Roles
                                                   join role in context.Roles on userRole.RoleId
                                                       equals role.Id
                                                   select role.Name).ToList()
                                  }).ToList().Select(p => new UserControlModel()

                                  {
                                      UserId = p.UserId,
                                      Username = p.Username,
                                      Email = p.Email,
                                      Role = string.Join(",", p.RoleNames)
                                  });
            var roleList = context.Roles
                .OrderByDescending(x => x.Id)
                .Select(x => x.Name).ToList();
            ViewBag.RoleList = roleList;
            return View(usersWithRoles);
        }

查看:

@using (Html.BeginForm("ModifyUser", "UserControl", FormMethod.Post, new { id = "ManageUser" }))
{

    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="box-title">
                <b>Users with Roles</b>
            </h3>
        </div>
        <div class="panel-body">
            <table class="table table-hover table-bordered table-condensed" id="UsersWithRoles">
                <thead>
                <tr>
                    <td><b>Username</b></td>
                    <td><b>Roles</b></td>
                </tr>
                </thead>
                @foreach (var user in Model)
                {
                    <tr>
                        <td>@user.Username</td>
                        <td>@user.Role</td>
                        <td>@Html.DropDownList("Roles", new SelectList(ViewBag.RoleList), "Modify Role", new { @class = "form-control" })</td>
                    </tr>
                }
            </table>
        </div>
    </div>
}

问题是我不仅需要发送在下拉列表中选择的数据,而且还需要发送用户ID的方法。 UserId是在我的方法中指定的,并通过user.UserId在表循环中引入 public string UserId { get; set; }

理想情况下,只需在下拉菜单中选择角色就可以轻松完成。

简而言之-每当下拉列表中选择一个角色时,我都需要一种方法来将我的下拉列表发送UserId和所选角色到我的控制器操作/方法中。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

这就是我要做的。

  1. 以HTML格式包装下拉列表(和其他字段)并为其指定ID
  2. 添加具有您要传递给服务器的用户ID值的隐藏输入
  3. 在下拉列表中添加onchange事件,该事件将调用javascript方法OnChangeFunc

@Html.DropDownList("Roles", new SelectList(ViewBag.RoleList), "Modify Role", new { @class = "form-control", @onchange="OnChangeFunc()" })

  1. JavaScript部分

    <script> function OnChangeFunc(){ //Get form by ID and submit it } </script>

答案 1 :(得分:1)

这是您可以使用的另一种设置,它为每一行设置表单。

在您看来,将现有表单替换为每行中的表单,以便您也可以在表单提交中传递用户ID。另外,该下拉菜单中还添加了“ onchange”,以便在更改时自动提交单个父表单。

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="box-title">
            <b>Users with Roles</b>
        </h3>
    </div>
    <div class="panel-body">
        <table class="table table-hover table-bordered table-condensed" id="UsersWithRoles">
            <thead>
                <tr>
                    <td><b>Username</b></td>
                    <td><b>Roles</b></td>
                </tr>
            </thead>
            @foreach (var user in Model)
            {
                <tr>
                    <td>@user.Username</td>
                    <td>@user.Role</td>
                    <td>
                        @using (Html.BeginForm("ModifyUser", "UserControl", FormMethod.Post, new { id = "ManageUser" }))
                        {
                            @Html.HiddenFor(modelItem => user.UserId)
                            @Html.DropDownList("Roles", new SelectList(ViewBag.RoleList), "Modify Role",
                                new { @class = "form-control", onchange = "this.parentElement.submit()" })
                        }
                    </td>
                </tr>
            }
        </table>
    </div>
</div>

现在,您可以从接收操作中获得角色和用户ID:

[AuthLog(Roles = "Super Admin")]
[HttpPost]
public ActionResult ModifyUser(string id)
{
    string newRole = Request.Form["Roles"];
    string userId = Request.Form["user.UserId"];
    // ...
}