/ Published in: C#
IMPORTANT: Be sure to clean the solution and rebuild after creating the WebMethod or you may get a 500 error.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
[WebMethod] public static string DeleteCaseNewsItem(int ID) { var newsItem = (from n in db.CaseNews where n.CaseNewsID == ID select n).FirstOrDefault(); if (newsItem == null) return "false"; db.CaseNews.DeleteOnSubmit(newsItem); db.SubmitChanges(); return "true"; } //and the js $('.delete_file').click(function () { if (confirm('Delete this file?')) { $(this).parent().slideUp('slow'); $.ajax({ type: "POST", url: "Manage.aspx/DeleteUploadedFile", data: "{ID: '" + $(this).attr('rel') + "' }", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { if (msg.d != "true") alert('There was an error deleting the file.'); }, error: function (response) { var error = $.parseJSON(response.responseText); alert('Sorry, an error occurred. Please contact support. The error was: ' + error.Message); } }); } return false; }); //--------------------------------- //a totally separate example getting a complex object returned in JSON. $(document).ready(function () { makeDropdownID = $('#ddModels').data('make-dropdown-id'); $('#' + makeDropdownID).change(function () { $.ajax({ type: "GET", url: "/services/Inventory.svc/GetModels", data: "inventoryType=A&make=" + $(this).val(), contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { if (data.d.length == 0) { //so user just unselected make and went back to "Select Make" on that dropdown. Or error occured and no models were found. $('#ddModels').empty().append('<option value="">Select Make First</option>'); } else { $('#ddModels').empty().append('<option value="">Select Model</option>'); } $.each(data.d, function () { $('#ddModels') .append($('<option></option>') .attr('value', this.Name) .text(this.Name)); }); } }); }); }); //Another separate example using JSON.stringify and json2.js library as a shim for older browsers that can't natively convert JS objects to JSON to send a complex object via ajax: $.ajax({ url: "ManageSegment.aspx/GetPricingPreview", data: "{'pricingRuleRequest':" + JSON.stringify(GetPricingRuleRequest()) + "}", success: function (response) { RenderPreview(response.d); }, error: function (response) { HandleAjaxError(response); } }); //Above example assumes ajax has been configured with default settings to keep the .ajax call simple, like this: //Sets some default configuration options for ajax calls to make jQuery ajax calls shorter. function InitAjaxSetup() { $.ajaxSetup({ type: "POST", contentType: "application/json; charset=utf-8", data: "{}", dataType: "json", error: function (response) { HandleAjaxError(response); } }); } //Handles any errors returned from ajax calls function HandleAjaxError(response) { var error = $.parseJSON(response.responseText); if (error.Message == 'Authentication failed.') { alert('Your session has timed out. You will now be redirected to the login page.'); window.location.reload(); return true; } alert('Sorry, an error occurred. Please contact support. The error was: ' + error.Message); }