i dynamically setting data attributes on widgets id's through javascript, attribute when go delete widget can remove widget entry database. have stepped through code in firebug , seems widgetid fine, when go make ajax post request not seem append id routing value.
here modal:
div class="modal fade" id="deletewidgetmodal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="mymodallabel">delete widget?</h4><!--add depending on panel have clicked--> </div> <div class="modal-body" id="mymodalbody"> <!--depending on panel insert content--> @using (html.beginform("deletewidgetconfirmed", "dashboard", formmethod.post, new { id = "__ajaxantiforgeryform" })) { @html.antiforgerytoken(); <div class="form-horizontal"> wish delete widget? <div class="form-group"> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">cancel</button> <button type="submit" value="deletewidgetconfirmed" class="btn btn-danger btn-ok" id="delete-widget">delete</button> </div> </div> </div> } </div> </div> </div>
here rendered html widget widgetid set:
<div class="panel panel-default" draggable="true" data-widgetid="4"> <div class="panel-heading"> <div class="panel-body">
i try make post:
$(document).ready(function () { $('#columns').on('click', '.glyphicon.glyphicon-trash', function (event) { var panel = this; //get id here //toggle modal $('#deletewidgetmodal').modal('show'); var widgetid = $(this).closest('.panel.panel-default').attr('data-widgetid'); document.getelementbyid('delete-widget').onclick = function (event) { event.stoppropagation(); //anti forgery token var form = $('#__ajaxantiforgeryform'); var token = $('input[name="__requestverificationtoken"]', form).val(); var url = '/dashboard/deletewidgetconfirmed'; console.log(widgetid + " test1"); //we make ajax call controller on click $.ajax({ url: url, data: { __requestverificationtoken: token, id: widgetid }, type: 'post', datatype: 'json', success: function(data){ var parentelement = $(panel).closest(".col-md-4.column"); var targetelement = $(panel).closest(".panel.panel-default"); targetelement.remove(); //parentelement.addclass("expand-panel"); checkemptypanelcontainers(); $('#deletewidgetmodal').modal('hide'); }, error: function (response) { console.log(widgetid + " error"); } }) } })
});
and here http post request got net panel in firebug:
/dashboard/deletewidgetconfirmed
and here controller:
// post: dashboardmodels/delete/5 [httppost] [validateantiforgerytoken] public actionresult deletewidgetconfirmed(int? id) { if(id == null) { return new httpstatuscoderesult(httpstatuscode.badrequest); } dashboardmodel dashboardmodel = db.dashboards.find(id); db.dashboards.remove(dashboardmodel); db.savechanges(); return new emptyresult(); }
here parameter being passed through response: http://gyazo.com/696b684cc3650dd24731ad8ecdce1447
Comments
Post a Comment