2 回答

TA貢獻1818條經驗 獲得超7個贊
這是您可以使用的另一個選項,它可以為每一行設置表單。
在您看來,將現有表單替換為每行中的一個表單,這樣您也可以在表單提交中傳入您的用戶 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"];
// ...
}

TA貢獻1834條經驗 獲得超8個贊
我就是這樣做的。
將下拉列表(和其他字段)包裝在 html 表單中并為其指定一個 ID
添加一個隱藏的輸入,其中包含要傳遞給服務器的用戶 ID 的值
在下拉列表中添加調用 javascript 方法 OnChangeFunc 的 onchange 事件
@Html.DropDownList("Roles", new SelectList(ViewBag.RoleList), "Modify Role", new { @class = "form-control", @onchange="OnChangeFunc()" })
Javascript部分
<script>
function OnChangeFunc(){
//Get form by ID and submit it
}
</script>
- 2 回答
- 0 關注
- 128 瀏覽
添加回答
舉報