这里500错误的最可能原因是什么?

时间:2016-02-02 08:13:42

标签: c# asp.net ajax asp.net-mvc tsql

我将向您展示所涉及的所有活动部件。

查看:

@{
    ViewBag.Title = "Partners";
}
<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <h1>Partners</h1>
        <p>Click to see survey answers or delete partner</p>
        <table class="table">
            <thead>
                <tr>
                    <th>Partner Name</th><th>Actions</th>
                </tr>
            </thead>
            <tbody>
                @foreach ( var NameIdPair in ViewBag.PartnersAndIds )
                {
                    <tr>
                        <td>
                            @NameIdPair.Name
                        </td>
                        <td>
                            <button class="btn btn-info view-partner-surveys" data-partnerid="@NameIdPair.Id">View Survey Answers</button>
                            <button class="btn btn-warning delete-partner" data-partnerid="@NameIdPair.Id">Delete Partner</button>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>
@section bottommost {
    <script type="text/javascript">
        $('.delete-partner').click(function () {
            var row = $(this).closest('tr');
            $.ajax({
                method: 'POST',
                url: 'DeletePartner',
                data: { pid: $(this).attr('data-partnerid') },
                dataType: 'json',
                processData: false,
                beforeSend: function () {
                    row.addClass('processing');
                },
                success: function (retinfo) {
                    if (retinfo.DeletedSuccessfully) { row.remove(); }
                    else { alert("Error .."); row.removeClass('processing'); }
                },
                error: function () { alert("Error"); row.removeClass('processing'); }
            });
        });
    </script>
}

使用$('.delete-partner').click调用的AJAX调用出现问题。处理请求的控制器很简单

[HttpPost]
public ActionResult DeletePartner ( int pid )
{
    return Json(new { DeletedSuccessfully = this._Db.DeletePartner(pid) });
}

定义的模型中使用方法DeletePartner
public bool DeletePartner ( int id )
{
    SqlCommand cmd = new SqlCommand("DeletePartner", this._Conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@id", id);
    this._Conn.Open();
    bool succeeded = cmd.ExecuteNonQuery() == 1 ? true : false;
    this._Conn.Close();
    return succeeded;
}

它的调用是简单的

CREATE PROCEDURE DeletePartner
    @id INT
AS
    DELETE FROM Partners WHERE id=@id

知道我哪里错了吗?

2 个答案:

答案 0 :(得分:0)

您应该使用url电话的ajax,如下所示。

url: '@Url.Action("DeletePartner")'

答案 1 :(得分:-1)

你必须以像

这样的格式提供ajax网址
url : '../controllerName/ActionName'


 [HttpPost]
public ActionResult DeletePartner ( int pid )
{
    return Json(new { DeletedSuccessfully = this._Db.DeletePartner(pid) });
}

控制器无法识别DeletedSuccessfully变量。所以它可能导致500错误