Hello everyone,
I'm working on a webapplication, ASP.Net MVC 4.0 with entityframework 6.0, trying to update database as per user selection. Data is sent to controller's action using jQuery AJAX. Below given is C# code to update entity which in turn updates database.
- public void modidyProduct(Productdetail prodData)
- {
- try
- {
- using (SampleTrialEntities entity = new SampleTrialEntities())
- {
- var data = entity.Productdetails.Where(p=>p.ProductID == prodData.ProductID).FirstOrDefault<Productdetail>();
- data.ProductName = prodData.ProductName;
- data.ProductNumber = prodData.ProductNumber;
- data.CategoryName = prodData.CategoryName;
- data.ModelName = prodData.ModelName;
- entity.Entry(data).State = System.Data.Entity.EntityState.Modified;
- entity.SaveChanges();
- }
- }
- catch (Exception)
- {}
- }
And here's jQuery AJAX call for that controller action method.- function updateProduct() {
- var productData = {
- ProductName: $('#prodName').val().trim(),
- ProductNumber: $('#prodNum').val().trim(),
- CategoryName: $('#ctgryName :selected').text(),
- ModelName: $('#mdlName :selected').text(),
- ProductID: atob($('#editProductId').val())
- };
- debugger;
- $('#divLoader').show();
- $.ajax({
- url: '@Url.Action("modidyProduct", "Home")',
- data: JSON.stringify(productData),
- type: 'POST',
- dataType: 'json',
- contentType: 'application/json;charset=utf-8',
- success: function (jqXHR) {
-
- $('#tblProducts').DataTable().destroy();
- $('#divLoader').hide();
- loadData();
- $('#addModal').modal('hide');
- $('#editProductId').val('');
- },
- error: function (msg) {
- debugger;
- $('#editProductId').val('');
- $('#divLoader').hide();
- alert(msg);
- alert("What's going wrong ?");
-
- }
- });
- }
- function loadData() {
- $('#divLoader').show();
-
- $.ajax({
- url: '@Url.Action("getProductDetails", "Home")',
- type: 'GET',
- dataType: 'json',
- success: function (data) {
- $('#tblProducts').DataTable({
- data: data,
- columns: [
- { 'data': 'ProductName' },
- { 'data': 'ProductNumber' },
- { 'data': 'CategoryName' },
- { 'data': 'ModelName' },
- {
- 'data': 'ProductID', "orderable": false, "render": function (data) {
- return '<a class="btn btn-warning" data-toggle="tooltip" id="deleteRecord" title="Delete"><i class="glyphicon glyphicon-trash"></i></a> <a class="btn btn-info" data-toggle="tooltip" id="editRecord" title="Edit" onclick= "getProductById(' + data + ',this);"><i class="glyphicon glyphicon-edit"></i></a>';
- }
- }]
- });
- $('#divLoader').hide();
- },
- error: function () {
- alert('Server couldn`t process request');
- }
- });
- }
After executing jQuery AJAX method & controllers action, successfully updates the record in datbase. Response statuscode - 200 & Status - OK is returned. But only error: { }, code block is executed everytime in AJAX method. It would be really helpful, if you guys post your comment/ response. Thanks in advance.