如何使用部分视图修复删除记录?

发布于 2025-01-24 10:53:37 字数 3173 浏览 2 评论 0原文

我想用模式从表中删除记录,但部分视图以模态显示。 我该如何解决这个问题? 而且我无法使用OnClick !!!

delete在控制器中删除的操作:

 public IActionResult DelPlatform(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }
        PR_Platform platformModel = _Iunit.PlatformsRepository.GenGetById(id);
        if (platformModel == null)
        {
           return NotFound();
        }
        return View(platformModel);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult DelPlatform(int id)
    {
        _Iunit.PlatformsRepository.GenDeleteById(id);
        return RedirectToAction(nameof(PlatformList));
    }

索引视图中的此删除按钮:

<a asp-controller="Platform" asp-action="DelPlatform" asp-route-id="@item.PlatformId" class="btn btn-danger btn-sm waves-effect waves-light" data-toggle="modal" data-target="#delete-modal"><i class="mdi mdi-delete" data-toggle="tooltip" data-placement="top" title="حذف"></i></a>

此删除脚本:

<script>
    (function ($) {
        function Delete() {
            var $this = this;
            function initilizeModel() {
                $("#delete-modal").on('show.bs.modal', function (e) {
                }).on('hidden.bs.modal', function (e) {
                    $(this).removeData('bs.modal');
                });
            }
            $this.init = function () {
                initilizeModel();
            }
        }
        $(function () {
            var self = new Delete();
            self.init();
        })
    }(jQuery))
</script>

此模式(在共享文件夹中):

<div class="modal fade bs-example-modal-center" tabindex="-1" role="dialog" id="delete-modal" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
        <div class="modal-header text-danger">
            <h5 class="modal-title mt-0 text-white"><strong>حذف از پایگاه داده</strong></h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        
        <div class="modal-footer mr-auto">
            <button type="submit" class="btn btn-primary waves-effect waves-light">حذف شود</button>
        </div>
    </div>
</div>

此删除操作视图(用于获取记录名称):

@model PR_Platform
@{
    Layout = null;
}
<form asp-controller="Platform" asp-action="DelPlatform" method="post">
    <div class="modal-body" id="delete-modal">
        <p><strong>آیا از حذف <em>"@Model.PlatformName"</em> مطمئن هستید؟</strong></p>
        <p class="mb-0"><small>@Model.PlatformName از پایگاه داده حذف خواهد شد</small></p>
    </div>
</form>

I want to delete the record from the table with modal but the partial view is not displayed in modal ...
How can I solve this problem !?
And that I can not use onClick !!!

enter image description here

this action for delete in controller:

 public IActionResult DelPlatform(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }
        PR_Platform platformModel = _Iunit.PlatformsRepository.GenGetById(id);
        if (platformModel == null)
        {
           return NotFound();
        }
        return View(platformModel);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult DelPlatform(int id)
    {
        _Iunit.PlatformsRepository.GenDeleteById(id);
        return RedirectToAction(nameof(PlatformList));
    }

this delete button in index view:

<a asp-controller="Platform" asp-action="DelPlatform" asp-route-id="@item.PlatformId" class="btn btn-danger btn-sm waves-effect waves-light" data-toggle="modal" data-target="#delete-modal"><i class="mdi mdi-delete" data-toggle="tooltip" data-placement="top" title="حذف"></i></a>

this script for delete:

<script>
    (function ($) {
        function Delete() {
            var $this = this;
            function initilizeModel() {
                $("#delete-modal").on('show.bs.modal', function (e) {
                }).on('hidden.bs.modal', function (e) {
                    $(this).removeData('bs.modal');
                });
            }
            $this.init = function () {
                initilizeModel();
            }
        }
        $(function () {
            var self = new Delete();
            self.init();
        })
    }(jQuery))
</script>

this modal (in shared folder):

<div class="modal fade bs-example-modal-center" tabindex="-1" role="dialog" id="delete-modal" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
        <div class="modal-header text-danger">
            <h5 class="modal-title mt-0 text-white"><strong>حذف از پایگاه داده</strong></h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">×</span>
            </button>
        </div>
        
        <div class="modal-footer mr-auto">
            <button type="submit" class="btn btn-primary waves-effect waves-light">حذف شود</button>
        </div>
    </div>
</div>

this delete action view (for get record name):

@model PR_Platform
@{
    Layout = null;
}
<form asp-controller="Platform" asp-action="DelPlatform" method="post">
    <div class="modal-body" id="delete-modal">
        <p><strong>آیا از حذف <em>"@Model.PlatformName"</em> مطمئن هستید؟</strong></p>
        <p class="mb-0"><small>@Model.PlatformName از پایگاه داده حذف خواهد شد</small></p>
    </div>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ花落谁相伴 2025-01-31 10:53:37

请尝试以下尝试:

查看:

@{
    ViewData["Title"] = "Home Page";
}

<div class="modal fade bs-example-modal-center" tabindex="-1" role="dialog" id="delete-modal" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header text-danger">
                <h5 class="modal-title mt-0 text-white"><strong>حذف از پایگاه داده</strong></h5>
                <form asp-controller="Platform" asp-action="DelPlatform" method="post">
                    <div class="modal-body" id="delete-modal">
                        @*<em>"@Model.PlatformName"</em> <small>@Model.PlatformName از پایگاه داده حذف خواهد شد</small>*@
                        <p><strong>آیا از حذف  مطمئن هستید؟</strong></p>
                        <p class="mb-0"></p>
                    </div>
                    <button type="submit">Click if you are sure to Delete</button>
                </form>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            @*<div class="modal-footer mr-auto">
                    <button type="submit" class="btn btn-primary waves-effect waves-light">حذف شود</button>
                </div>*@
        </div>
    </div>
</div>
@*asp-controller="Home" asp-action="DelPlatform" asp-route-id="@item.PlatformId"*@
<a class="btn btn-danger btn-sm waves-effect waves-light" data-toggle="modal" data-target="#delete-modal">Click to Delete<i class="mdi mdi-delete" data-toggle="tooltip" data-placement="top" title="حذف"></i></a>

”在此处输入图像描述”

Please try this:

View:

@{
    ViewData["Title"] = "Home Page";
}

<div class="modal fade bs-example-modal-center" tabindex="-1" role="dialog" id="delete-modal" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header text-danger">
                <h5 class="modal-title mt-0 text-white"><strong>حذف از پایگاه داده</strong></h5>
                <form asp-controller="Platform" asp-action="DelPlatform" method="post">
                    <div class="modal-body" id="delete-modal">
                        @*<em>"@Model.PlatformName"</em> <small>@Model.PlatformName از پایگاه داده حذف خواهد شد</small>*@
                        <p><strong>آیا از حذف  مطمئن هستید؟</strong></p>
                        <p class="mb-0"></p>
                    </div>
                    <button type="submit">Click if you are sure to Delete</button>
                </form>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            @*<div class="modal-footer mr-auto">
                    <button type="submit" class="btn btn-primary waves-effect waves-light">حذف شود</button>
                </div>*@
        </div>
    </div>
</div>
@*asp-controller="Home" asp-action="DelPlatform" asp-route-id="@item.PlatformId"*@
<a class="btn btn-danger btn-sm waves-effect waves-light" data-toggle="modal" data-target="#delete-modal">Click to Delete<i class="mdi mdi-delete" data-toggle="tooltip" data-placement="top" title="حذف"></i></a>

enter image description here

横笛休吹塞上声 2025-01-31 10:53:37

这是您可以遵循的整个工作演示:

模型:

public class PR_Platform
{
    public int PlatformId { get; set; }
    public string PlatformName { get; set; }
}

主视图(index.cshtml):

在锚标记中添加data-id以获取单击值。

@model IEnumerable<PR_Platform>
@foreach(var item in Model)
{
<a asp-controller="Platform" asp-action="DelPlatform" asp-route-id="@item.PlatformId" data-id="@item.PlatformId" class="btn btn-danger btn-sm waves-effect waves-light" data-toggle="modal" data-target="#delete-modal"><i class="mdi mdi-delete" data-toggle="tooltip" data-placement="top" title="حذف"></i></a>
<a asp-controller="Platform" asp-action="DelPlatform" asp-route-id="@item.PlatformId" data-id="@item.PlatformId" class="btn btn-danger btn-sm waves-effect waves-light" data-toggle="modal" data-target="#delete-modal"><i class="mdi mdi-delete" data-toggle="tooltip" data-placement="top" title="حذف"></i></a>
}   
<div class="modal fade bs-example-modal-center" tabindex="-1" role="dialog" id="delete-modal" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header text-danger">
                <h5 class="modal-title mt-0 text-white"><strong>حذف از پایگاه داده</strong></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                 //add this......
            </div>
            <div class="modal-footer mr-auto">
                <button type="submit" class="btn btn-primary waves-effect waves-light">حذف شود</button>
            </div>
        </div>
    </div>
</div>

主视图中的JS:

@section Scripts
{
    <script>
          (function ($) {
            function Delete() {
                var $this = this;
                function initilizeModel() {
                    $("#delete-modal").on('show.bs.modal', function (e) {
                        var $button = $(e.relatedTarget); //Button that triggered the modal
                        var id = $button.data("id");  //get the id
                        alert(id);
                        $.ajax({
                            type: "GET",
                            url: "/Platform/DelPlatform/"+id,                   
                            contentType: "application/json",
                            success: function (data) {
                                $(".modal-body").html(data);//load the response html code to the modal
                            },
                            error: function (e) {
                                alert('Error');
                            }
                        })
                    }).on('hidden.bs.modal', function (e) {
                        $(this).removeData('bs.modal');
                    });
               }
               $this.init = function () {
                    initilizeModel();
               }
           }
        $(function () {
            var self = new Delete();
            self.init();
        })
    }(jQuery))
    </script>
}

控制器:

public IActionResult DelPlatform(int? id)
{
    if (id == null)
    {
        return NotFound();
    }
    PR_Platform platformModel = _Iunit.PlatformsRepository.GenGetById(id);
    if (platformModel == null)
    {
        return NotFound();
    }
    return View(platformModel);
}

Here is a whole working demo you could follow:

Model:

public class PR_Platform
{
    public int PlatformId { get; set; }
    public string PlatformName { get; set; }
}

Main View(Index.cshtml):

Add data-id in the anchor tag for getting the clicked value.

@model IEnumerable<PR_Platform>
@foreach(var item in Model)
{
<a asp-controller="Platform" asp-action="DelPlatform" asp-route-id="@item.PlatformId" data-id="@item.PlatformId" class="btn btn-danger btn-sm waves-effect waves-light" data-toggle="modal" data-target="#delete-modal"><i class="mdi mdi-delete" data-toggle="tooltip" data-placement="top" title="حذف"></i></a>
<a asp-controller="Platform" asp-action="DelPlatform" asp-route-id="@item.PlatformId" data-id="@item.PlatformId" class="btn btn-danger btn-sm waves-effect waves-light" data-toggle="modal" data-target="#delete-modal"><i class="mdi mdi-delete" data-toggle="tooltip" data-placement="top" title="حذف"></i></a>
}   
<div class="modal fade bs-example-modal-center" tabindex="-1" role="dialog" id="delete-modal" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header text-danger">
                <h5 class="modal-title mt-0 text-white"><strong>حذف از پایگاه داده</strong></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                 //add this......
            </div>
            <div class="modal-footer mr-auto">
                <button type="submit" class="btn btn-primary waves-effect waves-light">حذف شود</button>
            </div>
        </div>
    </div>
</div>

JS in main view:

@section Scripts
{
    <script>
          (function ($) {
            function Delete() {
                var $this = this;
                function initilizeModel() {
                    $("#delete-modal").on('show.bs.modal', function (e) {
                        var $button = $(e.relatedTarget); //Button that triggered the modal
                        var id = $button.data("id");  //get the id
                        alert(id);
                        $.ajax({
                            type: "GET",
                            url: "/Platform/DelPlatform/"+id,                   
                            contentType: "application/json",
                            success: function (data) {
                                $(".modal-body").html(data);//load the response html code to the modal
                            },
                            error: function (e) {
                                alert('Error');
                            }
                        })
                    }).on('hidden.bs.modal', function (e) {
                        $(this).removeData('bs.modal');
                    });
               }
               $this.init = function () {
                    initilizeModel();
               }
           }
        $(function () {
            var self = new Delete();
            self.init();
        })
    }(jQuery))
    </script>
}

Controller:

public IActionResult DelPlatform(int? id)
{
    if (id == null)
    {
        return NotFound();
    }
    PR_Platform platformModel = _Iunit.PlatformsRepository.GenGetById(id);
    if (platformModel == null)
    {
        return NotFound();
    }
    return View(platformModel);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文