TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
L A
NA
170
170.9k
jQuery AJAX always executing error: {}
Oct 28 2017 5:23 PM
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) {
//Below line will destroy DataTable - tblProducts. So that we could bind table again. next line - loadData();
$(
'#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 ?"
);
//alert(jqXHR.responseText);
}
});
}
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.
Reply
Answers (
2
)
Set value to textbox array using jquery
Highlight default Li in Ul