javascript - Performing a delete in mvc-5 action through JQuery -
i have website i'm working on, user can add or delete booked traffics tram network. i've come far delete button.
<input type="button" value="delete" class="deletetraffic btn btn-link noborder nobackground" data-id="@traffic.id" />
jquery(document).ready(function() { jquery('.deletetraffic').click(function() { var id = $(this).data('id'); var url = '@url.action("deletetraffic", "trafficdate", new { trafficid=id })'; url = url.replace("id", id); $.post(url, function (data) { if (data) { $('#pnledittraffics').hide().fadein('fast'); } else { alert("error."); } }); }); });
public actionresult deletetraffic(int id) { return json(trafficdata.deletetraffic(id)); }
the button works fine , parameter comes through fine, action in controller never reached. controller named trafficdatecontroller
the first problem order in you're passing arguments url.action()
, action name first parameter while second controller name:
@url.action("deletetraffic", "trafficdate")
also, since deletetraffic
action meant invoked using post
verb, better pass id
part of request body hence avoiding need manually construct final path using url.replace('id', id)
:
var url = '@url.action("deletetraffic", "trafficdate")', data = { id: id }; $.post(url, data, function (data) { // ... });
see msdn
Comments
Post a Comment