1

I will show you all the moving parts involved.

View:

@{
    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>
}

The problem is occuring with the AJAX call invoked with $('.delete-partner').click. The controller handling the request is the simple

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

which used the method DeletePartner in a model defined by

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;
}

The sproc its calling is the simple

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

Any idea where I'm going wrong here?

M. Schena
  • 2,039
  • 1
  • 21
  • 29
user5648283
  • 5,913
  • 4
  • 22
  • 32
  • 1
    find the error by your self - look how : http://stackoverflow.com/questions/5385714/deploying-website-500-internal-server-error/5385884#5385884 – Aristos Feb 02 '16 at 08:17
  • have you checked the ajax url is working correctly. to insure it you have place a break point in the controller. – anand Feb 02 '16 at 08:43
  • 1
    `500 Internal Server Error`means your controller code is throwing and exception. You need to debug your code! And you can use your browser tools (Network tab) to inspect the response which will give details of the error. –  Feb 06 '16 at 02:42

2 Answers2

0

You should use the url of your ajax call like following.

url: '@Url.Action("DeletePartner")'
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
-1

You have to give ajax url in the format like

url : '../controllerName/ActionName'


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

The DeletedSuccessfully variable is not recognised by the controller. So it may cause 500 error

anand
  • 1,559
  • 5
  • 21
  • 45