Mô hình cửa sổ bật lên sử dụng jquery ajax chỉ cho một phần nhỏ của chế độ xem

lập trình


Vì vậy, tôi có chế độ xem có tên Chỉnh sửa người dùng và trong chế độ xem đó, tôi có phần mã này được cung cấp bên dưới và trong chế độ xem danh sách người dùng, tôi có nút có tên ROles trong phần Hành động, vì vậy điều tôi muốn là khi tôi nhấp vào nút đó, chỉ phần này sẽ bật lên như thế nào tôi có thể đạt được điều này giúp tôi về mặt này không. Cảm ơn trước

<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>

Đây là Mã hoàn chỉnh của Chế độ xem

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

Những gì tôi đã thử:

Tôi đã thử cách này bằng cách sử dụng Chế độ xem một phần nhưng không đạt được mục tiêu và không hiểu bất cứ điều gì vì tôi không nắm rõ Jquery ajax nên hãy gợi ý cho tôi giải pháp cho vấn đề này

Giải pháp 1

Trích dẫn:

Tôi đã thử cách này bằng cách sử dụng Chế độ xem một phần nhưng không đạt được mục tiêu và không hiểu bất cứ điều gì vì tôi không nắm rõ Jquery ajax nên hãy gợi ý cho tôi giải pháp cho vấn đề này

Những điều sau đây có thể khó nắm bắt theo sự thừa nhận của chính bạn. Tôi khuyên bạn nên đọc thêm để hiểu rõ hơn về tất cả, Google đã trả lại cho tôi những thông tin sau – Sử dụng AJAX để tải nội dung của phần mong muốn vào một phương thức[^]

Cách thực hiện điều này là thêm một vùng chứa phương thức vào HTML của bạn nơi nội dung sẽ được tải. Sau đó, sử dụng JavaScript để kích hoạt yêu cầu AJAX khi nhấp vào nút “Quản lý vai trò”. Bây giờ hãy tải nội dung HTML cụ thể vào vùng chứa phương thức –

HTML của bạn –

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) của bạn sẽ trông như thế này –

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>

Bạn sẽ cần thực hiện hành động nào đó trong Bộ điều khiển của mình –

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);
}

Bây giờ, hãy chạy HTML của bạn để hiển thị chế độ xem một phần cho ‘Vai trò người dùng’ của bạn –

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>

Hãy nhấp vào liên kết mà tôi đã cung cấp thay vì chỉ sao chép và dán mã của tôi và hy vọng tìm ra giải pháp, tôi chưa kiểm tra điều này nhưng nó sẵn sàng chỉ cho bạn đi đúng hướng.

コメント

タイトルとURLをコピーしました