PoliDev
PoliDev

Reputation: 1458

Delete the record using jquery when click submit button

I want to delete my record from database using jquery. when I save my file I use the following code,

The record Id's there..

Jobs

Controller Code

public ActionResult Delete(int id)
    {
        Job job = _repository.GetJob(id);
        if (job != null)
        {
            _repository.DeleteJobLanguages(id);
            _repository.DeleteJobLocations(id);
            _repository.DeleteJobPreferredIndustries(id);
            _repository.DeleteJobRequiredQualifications(id);
            _repository.DeleteJobRoles(id);
            _repository.DeleteJobskills(id);
            _repository.DeleteJob(id);
        }

        return View(job);

    }

Jquery Dial4Jobz.Job.Add = function (sender) { var form = $(sender).parent(); var data = form.serialize();

var url = form.attr('action');
$.ajax({
    type: "POST",
    url: url,
    data: data,
    dataType: "json",
    success: function (response) {
        Dial4Jobz.Common.ShowMessageBar(response.Message);
    },
    error: function (xhr, status, error) {
        Dial4Jobz.Common.ShowMessageBar(xhr.statusText);
    }
});
return false;

};

Here when I click submit button, it calls the jquery. Then it shows some error. How to write code in jquery for delete?

Upvotes: 0

Views: 1213

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You could have a server side controller action which will take the id of the record that needs to be deleted:

[HttpDelete]
public ActionResult Delete(int id)
{
    repository.Delete(id);
    return Json(new { id = id });
}

and then the same way you could use AJAX to invoke it:

$.ajax({
    type: "DELETE",
    url: url,
    data: { id: '123' }, // <-- put the id of the record you want to delete here
    success: function (response) {

    },
    error: function (xhr, status, error) {
    }
});

Upvotes: 2

Related Questions