[ad_1]
因此,我有一个名为“编辑用户”的视图,在该视图中,我在下面提供了这部分代码,在用户列表视图中,我在“操作”部分中有名为“ROles”的按钮,所以我想要的是,当我单击该按钮时,只有这部分应该如何弹出我可以实现这一目标吗?在这方面帮助我。 提前致谢
<div class="card"> <div class="card-header"> <h3>User Roles</h3> </div> <div class="card-body"> @if (Model.Roles.Any()) { foreach (var role in Model.Roles) { <h5 >@role</h5> } } else { <h5 class="card-title">None at the moment</h5> } </div> <div class="card-footer"> <a asp-action="ManageUserRoles" asp-route-userId="@Model.Id" class="btn btn-primary"> Manage Roles </a> </div> </div>
这是视图的完整代码
@model EditUserViewModel @{ ViewBag.Title = "Edit User"; Layout = "~/Views/Shared/AdminLTE/_Layout.cshtml"; } <h1>Edit User</h1> <form method="post" class="mt-3"> <div class="form-group row"> <label asp-for="Id" class="col-sm-2 col-form-label"></label> <div class="col-sm-10"> <input asp-for="Id" disabled class="form-control"> </div> </div> <div class="form-group row"> <label asp-for="FirstName" class="col-sm-2 col-form-label"></label> <div class="col-sm-10"> <input asp-for="FirstName" class="form-control"> </div> </div> <div class="form-group row"> <label asp-for="LastName" class="col-sm-2 col-form-label"></label> <div class="col-sm-10"> <input asp-for="LastName" class="form-control"> </div> </div> <div class="form-group row"> <label asp-for="Email" class="col-sm-2 col-form-label"></label> <div class="col-sm-10"> <input asp-for="Email" class="form-control"> <span asp-validation-for="Email" class="text-danger"></span> </div> </div> <div class="form-group row"> <label asp-for="UserName" class="col-sm-2 col-form-label"></label> <div class="col-sm-10"> <input asp-for="UserName" class="form-control"> <span asp-validation-for="UserName" class="text-danger"></span> </div> </div> <div asp-validation-summary="All" class="text-danger"></div> <div class="form-group row"> <div class="col-sm-10"> <button type="submit" class="btn btn-primary">Update</button> <a asp-action="ListUsers" class="btn btn-primary">Cancel</a> </div> </div> <div class="card"> <div class="card-header"> <h3>User Roles</h3> </div> <div class="card-body"> @if (Model.Roles.Any()) { foreach (var role in Model.Roles) { <h5 >@role</h5> } } else { <h5 class="card-title">None at the moment</h5> } </div> <div class="card-footer"> <a asp-action="ManageUserRoles" asp-route-userId="@Model.Id" class="btn btn-primary"> Manage Roles </a> </div> </div> <div class="card mt-3"> <div class="card-header"> <h3>User Claims</h3> </div> <div class="card-body"> @if (Model.Claims.Any()) { foreach (var claim in Model.Claims) { <h5 >@claim</h5> } } else { <h5 class="card-title">None at the moment</h5> } </div> <div class="card-footer"> <a asp-action="ManageUserClaims" asp-route-userId="@Model.Id" style="width:auto" class="btn btn-primary"> Manage Claims </a> </div> </div> </form>
我尝试过的:
我已经尝试使用部分视图,但没有实现目标,并且不理解任何东西,因为我对 Jquery ajax 没有很好的掌握,所以建议我解决这个问题
解决方案1
引用:我已经尝试使用部分视图,但没有实现目标,并且不理解任何东西,因为我对 Jquery ajax 没有很好的掌握,所以建议我解决这个问题
根据您自己的说法,以下内容可能很难理解。 我建议你阅读更多内容以了解这一切,谷歌为我返回了以下内容 – 使用 AJAX 将所需部分的内容加载到模态中[^]
解决这个问题的方法是在 HTML 中添加一个模式容器,内容将在其中加载。 然后,当单击“管理角色”按钮时,使用 JavaScript 触发 AJAX 请求。 现在将特定的 HTML 内容加载到模式容器中 –
您的 HTML –
超文本标记语言
!-- Add your modal container... --> <div class="modal fade" id="manageRolesModal" tabindex="-1" role="dialog" aria-labelledby="manageRolesModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="manageRolesModalLabel">User Roles</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body" id="manageRolesModalBody"> <!-- The content that you want to see as per your post will be loaded here... --> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> </div> </div> </div> </div>
你的 JavaScript(jQuery) 将如下所示 –
JavaScript
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <script> $(document).ready(function() { //Handle the click event for your "Manage Roles" button... $('a[data-toggle="manageRolesModal"]').click(function() { var userId = $(this).data('userid'); //Now make an AJAX request to load the content you want... $.ajax({ url: '/YourControllerNameHere/ManageUserRolesPartial', type: 'GET', data: { userId: userId }, success: function(data) { $('#manageRolesModalBody').html(data); //Load all the content into your created modal body... $('#manageRolesModal').modal('show'); //Now show your modal... }, error: function(xhr, status, error) { console.error(error); } }); }); }); </script>
您需要在控制器中执行某些操作 –
C#
public IActionResult ManageUserRolesPartial(string userId) { //Fetch roles for the user selected with the 'userId' that was returned... //I am assuming you have a method to retrieve roles based on userId... var roles = _userService.GetUserRoles(userId); return PartialView("_UserRolesPartial", roles); }
现在,运行 HTML 以显示“用户角色”的部分视图 –
超文本标记语言
<div class="card"> <div class="card-header"> <h3>User Roles</h3> </div> <div class="card-body"> @if (Model.Any()) { foreach (var role in Model) { <h5>@role</h5> } } else { <h5 class="card-title">None at the moment</h5> } </div> <div class="card-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> </div> </div>
请按照我提供的链接进行操作,而不仅仅是复制和粘贴我的代码并希望找到解决方案,我还没有对此进行测试,但它准确地为您指明了正确的方向。
[ad_2]
コメント