【发布时间】:2022-01-01 04:39:48
【问题描述】:
AJAX 函数没有将 Id 参数传递给我的控制器中的 GET 方法。
我有这张桌子。
@foreach (var user in Model)
{
<tr>
<td>@user.FirstName</td>
<td>@user.LastName</td>
<td>@user.Email</td>
<td>@string.Join(" , ", user.Roles.ToList())</td>
<td>
<a class="btn btn-primary" onclick="manageRolePopup('@Url.Action("Manage","Role", new {id=user.UserId },Context.Request.Scheme)')">Manage Roles</a>
<partial name="_RoleManagePopup.cshtml" />
</td>
</tr>
}
点击时我想在弹出用户的名字和姓氏中显示,所以我在我的 Controller
中有这个 [HttpGet]
public async Task<IActionResult> Manage(string userId)
{
var user = await _userManager.FindByIdAsync(userId);
ViewBag.FirstName = user.FirstName;
ViewBag.LastName = user.LastName;
var model = new ManageRoleViewModel();
List<string> roleNames = new List<string>();
foreach(var role in _roleManager.Roles)
{
model.RoleId = role.Id;
roleNames.Add(role.Name);
}
model.UserId = user.Id;
model.RoleNames = roleNames;
return View(model);
}
AJAX
manageRolePopup = (url) => {
$.ajax({
type: "GET",
url: url,
success: function (res) {
$("#form-modal .modal-body").html(res);
$("#form-modal").modal("show");
}
})
}
查看
<form method="post" asp-controller="Role" asp-action="Manage" asp-route-UserId="@Model.UserId">
<div class="row">
<div class="col-3">
<h4>@ViewBag.FirstName @ViewBag.LastName</h4>
<div class="form-group">
<select asp-items="@new SelectList(Model.RoleNames)">
<option selected disabled>---Select New Role---</option>
</select>
</div>
</div>
</div>
</form>
当我像下面一样通过 Id 时,一切都很好。用户不为空,id参数也是。
<a asp-controller="Role" asp-action="Manage" asp-route-UserId="user.UserId"></a>
显然我想做 UPDATE 方法,但现在我只想让它显示。
【问题讨论】:
标签: c# asp.net ajax asp.net-mvc asp.net-ajax