Ajax调用的动态单击事件导致状态为500

时间:2017-07-24 15:23:12

标签: jquery ajax asp.net-mvc webgrid

我在MVC中有一个web网格,自动调用ajax进行分页。 我正在使用Jquery Ajax调用从webgrid中删除一行。

问题是当我通过Ajax删除功能时,大多数时候我得到500内部服务器错误。我不确定导致错误的原因。 注意:如果我在调试模式下,那么我没有遇到任何错误。

以下是我的代码:

Webgrid HTML页面:

  <div class="row panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title"><b>All Recipients</b></h3>
        </div>
        <div class="panel-body">

            <div id="SearchRecipientGrid">
                @MvcHtmlString.Create(
                @grid.GetHtml(
                    htmlAttributes: new
                    {
                        id = "Grid",
                        @class = "table table-bordered table-striped table-condensed"
                    },
                    mode: WebGridPagerModes.All,
                    firstText: "First",
                    previousText: "Prev",
                    nextText: "Next",
                    lastText: "Last",
                    columns: grid.Columns
                        (
                            grid.Column("", "View", format: @<text> <a href="@Url.Action("ViewRecipient", "Recipient", new {id = @item.UniqueKey})" class="btn">
                                <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
                            </a> </text>),
                            grid.Column("", "Update", format: @<text> <a href="@Url.Action("UpdateRecipient", "Recipient", new { id = @item.UniqueKey })" class="btn">
                        <span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
                    </a> </text>),
                            grid.Column("", "Delete", format: @<text> <a href="#"
                                                                         id="@item.RecipientsID" class="btn deleteRecipient" value="@item.Name">
                        <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                    </a> </text>),
grid.Column("Name", "Name"),
grid.Column("MedicaidNumber", "Medicaid Number"),
grid.Column("SSN", "SSN"),
grid.Column("CaseNumber", "Case Number"),
grid.Column("County", "County"),
grid.Column("LienStatus", "LienStatus")
)
).ToString().Replace("<tfoot>",
"<tfoot><tr class='footstuff'><td colspan='9' style='color: black;'>Page " + (grid.PageIndex + 1) + " of " + ((grid.TotalRowCount / grid.RowsPerPage) + 1) + "</td>"))
            </div>
        </div>
    </div>

jQuery Ajax调用:

注意:如果我不使用&#39;文件&#39;对于点击事件,我无法删除分页上的功能

   //It is used to delete a property from web grid
    $(document).on('click', '.deleteRecipient', function () {
        var id = $(this).attr('id');
        var name = $.trim($(this).attr('value'));
        var paramObj = { id :id, name :name }
        window.baseShowModalOkCancelWithFunction("Delete Recipient"
            , "<p>Are you sure to delete Recipient " + name + "</p>"
            , deleteRecipient
            , ""
            , paramObj
            , ""
            , "INFORMATIONAL");

    });

    function deleteRecipient(paramObj) {
        var data = { "id": paramObj.id };
        var url = "@Url.Action("DeleteRecipient", "Recipient")?id=" + data.id;
        console.log(url);
        $.ajax({
            url: url,
            type: 'POST',
            dataType: "json",
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                if (result === true) { // Successfully Deleted
                    window.baseCustomAutoClose("Recipient " + name + " was successfully deleted from the database.");
                    $("#" + id).closest("tr").remove();
                }

            },
            error: function () {
                window.baseShowModalOkCancel("<p>Recipient NOT deleted</p", "<p>Recipient Name: " + paramObj.name + " encountered a database error</p>", "ERROR");
            }

        });
    }

控制器代码:

      public ActionResult SearchRecipient(RecipientSearchModel model)
    {
        using (var db = new LiensTrackerEntities())
        {
            List<SearchRecipientView> modelRec = new List<SearchRecipientView>();
            RecipientSearchModel viewData = new RecipientSearchModel()
            {

                AllDropDowns = LiensHelper.GetAllDropDown(),
                RecipientsList = modelRec
            };

            if (!string.IsNullOrEmpty(model.SearchTypeText))
            {
                if (model.SearchType == "1")
                {
                    viewData.RecipientsList = db.SearchRecipientViews.Where(x => x.Name.ToLower().Contains(model.SearchTypeText)).ToList();

                }
                else if (model.SearchType == "2")
                {
                    viewData.RecipientsList = db.SearchRecipientViews.Where(x => x.MedicaidNumber == model.SearchTypeText).ToList();

                }
                else if (model.SearchType == "3")
                {
                    viewData.RecipientsList = db.SearchRecipientViews.Where(x => x.SSN == model.SearchTypeText).ToList();

                }
                else if (model.SearchType == "4")
                {
                    viewData.RecipientsList = db.SearchRecipientViews.Where(x => x.County == model.SearchTypeText).ToList();

                }
                else if (model.SearchType == "5")
                {
                    viewData.RecipientsList = db.SearchRecipientViews.Where(x => x.MedicaidNumber == model.SearchTypeText).ToList();

                }

            }
            else if (model.SearchType == null)
            {
                viewData.RecipientsList = db.SearchRecipientViews.ToList();
            }
            return View(viewData);
        }
    }


    [HttpPost]
    public ActionResult DeleteRecipient(int id)
    {
        using (var db = new LiensTrackerEntities())
        {
            using (var transaction = db.Database.BeginTransaction())
            {
                Recipient  recipient = db.Recipients.FirstOrDefault(x => x.RecipientsID == id);
                if (recipient != null)
                {
                    recipient.DeleteFlag = true;
                    db.SaveChanges();
                    transaction.Commit();
                    return Json(true, JsonRequestBehavior.AllowGet);
                }

                return View("Error");
            }

        }

    }

0 个答案:

没有答案