在更改时使用 DropDownList 传递多个数据字段

我目前正在尝试创建修改用户角色页面。以下是我到目前为止所取得的成就......

例子

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

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

@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()

                                  })


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


理想情况下,这将通过在下拉列表中选择角色的简单更改来完成。


简而言之——只要在下拉列表中选择了一个角色,我就需要一种方法让我的下拉列表将 UserId 和所选角色发送到我的控制器操作/方法。


任何帮助将不胜感激。


元芳怎么了
浏览 164回答 2
2回答

qq_笑_17

这是您可以使用的另一个选项,它可以为每一行设置表单。在您看来,将现有表单替换为每行中的一个表单,这样您也可以在表单提交中传入您的用户 ID。此外,下拉列表添加了“onchange”,以便在更改时自动提交单个父表单。<div class="panel panel-primary">&nbsp; &nbsp; <div class="panel-heading">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="box-title">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>Users with Roles</b>&nbsp; &nbsp; &nbsp; &nbsp; </h3>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="panel-body">&nbsp; &nbsp; &nbsp; &nbsp; <table class="table table-hover table-bordered table-condensed" id="UsersWithRoles">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><b>Username</b></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><b>Roles</b></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach (var user in Model)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@user.Username</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@user.Role</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @using (Html.BeginForm("ModifyUser", "UserControl", FormMethod.Post, new { id = "ManageUser" }))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.HiddenFor(modelItem => user.UserId)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.DropDownList("Roles", new SelectList(ViewBag.RoleList), "Modify Role",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new { @class = "form-control", onchange = "this.parentElement.submit()" })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; </div></div>现在您可以从接收操作中获取角色和用户 ID:[AuthLog(Roles = "Super Admin")][HttpPost]public ActionResult ModifyUser(string id){&nbsp; &nbsp; string newRole = Request.Form["Roles"];&nbsp; &nbsp; string userId = Request.Form["user.UserId"];&nbsp; &nbsp; // ...}

MMMHUHU

我就是这样做的。将下拉列表(和其他字段)包装在 html 表单中并为其指定一个 ID添加一个隐藏的输入,其中包含要传递给服务器的用户 ID 的值在下拉列表中添加调用 javascript 方法 OnChangeFunc 的 onchange 事件@Html.DropDownList("Roles", new SelectList(ViewBag.RoleList), "Modify Role", new { @class = "form-control", @onchange="OnChangeFunc()" })Javascript部分<script>&nbsp; &nbsp; function OnChangeFunc(){&nbsp; &nbsp; &nbsp; &nbsp; //Get form by ID and submit it&nbsp; &nbsp; }</script>
打开App,查看更多内容
随时随地看视频慕课网APP