Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的应用程序上的“删除”按钮不起作用?_Javascript_C#_.net_Asp.net Mvc - Fatal编程技术网

Javascript 为什么我的应用程序上的“删除”按钮不起作用?

Javascript 为什么我的应用程序上的“删除”按钮不起作用?,javascript,c#,.net,asp.net-mvc,Javascript,C#,.net,Asp.net Mvc,我制作了一个删除按钮,该按钮用于删除模型中的一些条目,但我不断收到一个错误,我不知道如何解决,我有一个控制器,它有一个名为deletelname的IActionresult,所有内容看起来都很合逻辑,但我只是不知道为什么它不起作用。如果有人能看一下代码并给我一些反馈,我将非常感激 观点: @model IEnumerable<Models.pinfo> @using Models @foreach (var s in Model){ <h1> @s.fname @s

我制作了一个删除按钮,该按钮用于删除模型中的一些条目,但我不断收到一个错误,我不知道如何解决,我有一个控制器,它有一个名为deletelname的IActionresult,所有内容看起来都很合逻辑,但我只是不知道为什么它不起作用。如果有人能看一下代码并给我一些反馈,我将非常感激

观点:

 @model IEnumerable<Models.pinfo>
@using Models
@foreach (var s in Model){

<h1> @s.fname  @s.lname </h1> <h3> @s.comment </h3> <a class="GoDelete" href="javascript:void(0)" data-id="@s.lname">Delete</a>

   <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Deleting....</h4>
                </div>
                <div class="modal-body">
                    Are you sure to Delete this Course?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button id="btndelete" type="button" class="btn btn-primary">Delete</button>
                </div>
            </div>
        </div>
    </div>

}

@section scripts
{
    <script>
        $(document).ready(function() {
            $("#btndelete").click(function () {
                $('#myModal').modal('hide');
                var id = $('#hfId').val();
                window.location.href = '@Url.Action("deletelname","Home")/'+id;
            });

            $(".GoDelete").click(function () {
                var id = $(this).attr("data-id");
                $('#hfId').val(id);
                $('#myModal').modal('show');
            });

        });
    </script>


}
以下是我按下删除按钮时出现的错误:


错误消息是因为您正在传递字符串“Home”作为视图“Contact”的模型。视图需要Model.pinfo类型的可枚举对象

渲染视图时,尝试将pinfo对象从控制器中传回。我不确定这会满足你的要求,但这似乎是你想要的

    public IActionResult deletelname(pinfo pinfo)
    {

        var fsname = db.pinfo;
        var lsname = db.pinfo;
        var csomment = db.pinfo;

        foreach (var fname in fsname)
        {
            db.Remove(fname);
        }

        foreach (var lname in lsname){
            db.Remove(lname);
        }

        return Contact();
    }
Javascript也不正确

$("#btndelete").click(function () {
    $.ajax({
        url: '@Url.Action("deletelname","Home")',
        method: 'POST',
        data: { pinfo: $('#hfId').val() },
        success: function (response) {
            // this code gets run after the request is successful
            // based on the controller method that we have written, the response
            // should be all of the "Contact" view html re-rendered.
            console.log(response);

            //if there is an error you will probably not want to close the modal
            // so only close if it is successful
            $('#myModal').modal('hide');
        },
        error: function (response) {
            //runs if there is an error
            console.log(response);
        }
    });
});

你好,朋友,似乎我打错了,但现在在修复它之后,我在chrome中得到了一个完全不同的错误,我将感谢您的帮助。HtmlButtoneElement未定义错误:Uncaught ReferenceError:pinfoId。(联系人:77)在HTMLButtonElement.dispatch(jquery.js:4737)在HTMLButtonElement.elemData.handle(jquery.js:4549)只是为了澄清一下,上面显示的视图代码的文件名是什么?它的联系人。cshtml@Dr.MaxCox忘记了模型需要的是可枚举的,而不是模型的实例。我更新了我的答案。嘿,非常感谢你的帮助,它会删除它,但当我刷新页面时,数据会重新打开…这是有道理的,因为我让你传回从数据库中删除的模型。当它从数据库中删除时,实际上并没有更改模型。您最初是如何显示视图的?您可以发布控制器方法吗?当然可以:public IActionResult Contact(){ViewData[“Message”]=“您的联系人页面”。.var liste=db.pinfo.ToList();return View(liste);}请立即尝试。我将控制器方法更改为使用您最初为填充而进行的数据库调用。我认为这应该行得通。
$("#btndelete").click(function () {
    $.ajax({
        url: '@Url.Action("deletelname","Home")',
        method: 'POST',
        data: { pinfo: $('#hfId').val() },
        success: function (response) {
            // this code gets run after the request is successful
            // based on the controller method that we have written, the response
            // should be all of the "Contact" view html re-rendered.
            console.log(response);

            //if there is an error you will probably not want to close the modal
            // so only close if it is successful
            $('#myModal').modal('hide');
        },
        error: function (response) {
            //runs if there is an error
            console.log(response);
        }
    });
});