post form data and bind with model : using jquery ajax in mvc
There are multiple ways of posting data from view to controller using jquery + ajax in mvc
1. post data by assigning parameters one by one and bind it with model manually
2. post data by serializing form and automatically binding with model
3. post data by assigning parameters one by one + making custom class and assigning data to its variables which are named similarly as that of model
Method 1:
$.ajax({
url: "/Controller/SubmitAction",
type: "POST",
dataType: "text",
data: { name: _name, age: _age, id: _id },
success: function (response) {
alert("Success");
},
error: function () {
alert("Error");
}
});
public JsonResult SubmitAction(string name, string age, int id)
{
try
{
Person obj = context.Person.FirstOrDefault(x=>x.id = id);
obj.Name = name;
obj.Age = age;
context.SaveChanges();
return Json("Success");
}
catch (Exception ex)
{
return Json("Failure");
}
}
Method 2:
var dataToPost = $("#myForm").serialize();
$.ajax({
url: "/Controller/SubmitAction",
type: "POST",
dataType: "text",
data: JSON.stringify(dataToPost),
success: function (response) {
alert("Success");
},
error: function () {
alert("Error");
}
});
public JsonResult SubmitAction(Person obj)
{
try
{
context.AttachCopy(obj);
context.SaveChanges();
return Json("Success");
}
catch (Exception ex)
{
return Json("Failure");
}
}
Method 3:
//GETTING VALUES
var _name = $("[name='name']").val();
var _age = $("[name='age']").val();
//CREATING CLASS OBJECT & ASSIGNING VALUES
var dataToPost = {
name : _name ,
age : _age
};
$.ajax({
url: "/Controller/SubmitAction",
type: "POST",
dataType: "text",
data: { cdata: dataToPost, id: _id },
success: function (response) {
alert("Success");
},
error: function () {
alert("Error");
}
});
public JsonResult SubmitAction(Person cdata, int id)
{
try
{
Person obj = context.Person.FirstOrDefault(x=>x.id = id);
obj.Name = cdata.Name;
obj.Age = cdata.Age;
context.SaveChanges();
return Json("Success");
}
catch (Exception ex)
{
return Json("Failure");
}
}
1. post data by assigning parameters one by one and bind it with model manually
2. post data by serializing form and automatically binding with model
3. post data by assigning parameters one by one + making custom class and assigning data to its variables which are named similarly as that of model
Method 1:
$.ajax({
url: "/Controller/SubmitAction",
type: "POST",
dataType: "text",
data: { name: _name, age: _age, id: _id },
success: function (response) {
alert("Success");
},
error: function () {
alert("Error");
}
});
public JsonResult SubmitAction(string name, string age, int id)
{
try
{
Person obj = context.Person.FirstOrDefault(x=>x.id = id);
obj.Name = name;
obj.Age = age;
context.SaveChanges();
return Json("Success");
}
catch (Exception ex)
{
return Json("Failure");
}
}
Method 2:
var dataToPost = $("#myForm").serialize();
$.ajax({
url: "/Controller/SubmitAction",
type: "POST",
dataType: "text",
data: JSON.stringify(dataToPost),
success: function (response) {
alert("Success");
},
error: function () {
alert("Error");
}
});
public JsonResult SubmitAction(Person obj)
{
try
{
context.AttachCopy(obj);
context.SaveChanges();
return Json("Success");
}
catch (Exception ex)
{
return Json("Failure");
}
}
Method 3:
//GETTING VALUES
var _name = $("[name='name']").val();
var _age = $("[name='age']").val();
//CREATING CLASS OBJECT & ASSIGNING VALUES
var dataToPost = {
name : _name ,
age : _age
};
$.ajax({
url: "/Controller/SubmitAction",
type: "POST",
dataType: "text",
data: { cdata: dataToPost, id: _id },
success: function (response) {
alert("Success");
},
error: function () {
alert("Error");
}
});
public JsonResult SubmitAction(Person cdata, int id)
{
try
{
Person obj = context.Person.FirstOrDefault(x=>x.id = id);
obj.Name = cdata.Name;
obj.Age = cdata.Age;
context.SaveChanges();
return Json("Success");
}
catch (Exception ex)
{
return Json("Failure");
}
}
Comments
Post a Comment