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

时间:2017-06-21 20:56:02

标签: javascript c# .net asp.net-mvc

我做了一个删除按钮,它应该从模型中删除一些条目,但我一直得到一个错误,我不知道如何解决,我有一个控制器,有一个名为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>


}

控制器部分:

    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 View("Contact", "Home");
    }

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

error

1 个答案:

答案 0 :(得分:3)

错误消息是因为您将字符串“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);
        }
    });
});