I have the following entity
Entity AssetMgmt
public class AssetMgmt : CommonPropertiesViewModel
{
public int DepartmentId { get; set; }
public int TypeOfDevice { get; set; }
public string DeviceStatus { get; set; }
public test[] Subscriptions { get; set; }
}
Entity test
public class test
{
public string IMEINumber { get; set; }
public int? SubscriptionType { get; set; }
public string SubscriptionTypeName { get; set; }
public int CarrierId { get; set; }
public string CarrierName { get; set; }
public int AccountId { get; set; }
public string PhoneNo { get; set; }
public bool setAsPrimary { get; set; }
public string SIMNo { get; set; }
public string Puk1 { get; set; }
public string Puk2 { get; set; }
public int Id { get; set; }
public decimal Maxbudget { get; set; }
public bool SendConsumption { get; set; }
}
Entity CommonPropertiesViewModel
public class CommonPropertiesViewModel
{
public int Id { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
public string CustomMessage { get; set; }
public int ClientId { get; set; }
}
javascript function
function SaveNew() {
var del = { "ClientId" : 0,
"CreatedBy" : null,
"CreatedOn" : "/Date(-62135596800000)/",
"CustomMessage" : null,
"DepartmentId" : 0,
"DeviceStatus" : null,
"Id" : 4,
"ModifiedBy" : null,
"ModifiedOn" : null,
"Subscriptions" : [ { "AccountId" : 421,
"CarrierId" : 1,
"CarrierName" : "Airtel",
"IMEINumber" : "352698040297280",
"Id" : 8,
"Maxbudget" : 250.0,
"PhoneNo" : "9409635039",
"Puk1" : "2415367892",
"Puk2" : "9818067434",
"SIMNo" : "485769351624",
"SendConsumption" : true,
"SubscriptionType" : 1,
"SubscriptionTypeName" : "Voice",
"setAsPrimary" : true
},
{ "AccountId" : 421,
"CarrierId" : 1,
"CarrierName" : "Airtel",
"IMEINumber" : "352276053001012",
"Id" : 9,
"Maxbudget" : 750.0,
"PhoneNo" : "4875351547",
"Puk1" : "9742277228",
"Puk2" : "2789210574",
"SIMNo" : "365289701254",
"SendConsumption" : true,
"SubscriptionType" : 1,
"SubscriptionTypeName" : "Voice",
"setAsPrimary" : true
}
],
"TypeOfDevice" : 1
}
$.ajax({
type: "POST",
url: $("#urlprefix").val() + 'AssetManagement/SaveNew',
data: { "del": JSON.stringify(del) },
success: function (d) {
console.log('here');
return false;
},
error: function (msg) {
}
});
}
After doing all these... My AJAX Call
[HttpPost]
public ActionResult SaveNew(AssetMgmt del)
{
return View("SaveNew", del);
}
When i hover on highlighted area in debug mode I should get values that are sent from client side to the server.
My problem is that when i make AJAX Call I am getting no value for del. No values are received on server side. IE in SaveNew(AssetMgmt del) i get either null or blank object. What i want is value for del that is sent from client side.
you need to spe4cify the content type as JSON
$.ajax({
type: "POST",
url: $("#urlprefix").val() + 'AssetManagement/SaveNew',
data: JSON.stringify({ del: del }),
contentType: 'application/json',
success: function (d) {
console.log('here');
return false;
},
error: function (msg) {
}
});
you can get value from this way
$.ajax({
type: "POST",
url: $("#urlprefix").val() + 'AssetManagement/SaveNew',
data: JSON.stringify({ del: del }),
contentType: 'application/json',
success: function (d) {
alert(d.del);
console.log('here');
return false;
},
error: function (msg) {
}
});
See this simple example and try that way
This is json function
function SaveNew() {
var AccountId = 421;
$.ajax({
url: 'SaveNew',
type: 'POST',
data: JSON.stringify({ AccountId : AccountId }),
dataType: 'json',
contentType: 'application/json',
success: function (result) {
if(result.del==1)
{
alert('record saved Successfully.');
}
},
error: function () {
alert('Error Occurred');
}
});
}
};
and this is controller code
[HttpPost]
public JsonResult SaveNew(int AccountId )
{
//here is save code
var del=1;
return Json(new { del }, JsonRequestBehavior.AllowGet);
}
This is perfect work now u can add more fields like 'AccountId' and pass to controller.
first add list in Model of all model class
public class AdminList
{
public List<AssetMgmt> ObjAssetMgmtList { get; set; }
public List<test> ObjtestList { get; set; }
public List<CommonPropertiesViewModel> ObjCommonPropertiesList { get; set; }
}
Then
[HttpPost]
public JsonResult SaveNew(int AccountId )
{
AdminList admins1 = new AdminList();
//add list in admins1 whatever u want then return
return Json(new { admins1 }, JsonRequestBehavior.AllowGet);
}
First u take array in javascript function like
function SaveNew() {
var AccountId;
var CarrierId;
var CarrierName;
var del= [];
del[0] = {
AccountId : 421,
CarrierId : 1,
CarrierName: "Airtel"
};
}
$.ajax({
url: 'SaveNew',
type: 'POST',
data: JSON.stringify({ del: del}),
dataType: 'json',
contentType: 'application/json',
success: function (result) {
//Now u get all field value return back that add in del
int id=result.del.AccountId;
},
error: function () {
alert('Error Occurred');
}
});
}
};
Then add in controller
[HttpPost]
public JsonResult SaveNew(AdminList[] del)
{
AdminList admins1 = new AdminList();
//here u can get field that added in del like
int id=del[0].AccountId;
return Json(new { admins1 }, JsonRequestBehavior.AllowGet);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.