[英]Unable to retrieve value in AJAX POST call. [Used AJAX, MVC ASP.net, .net framework 4.0]
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 实体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 javascript功能
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 完成所有这些操作后...我的AJAX通话
[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. 我的问题是,当我拨打AJAX电话时,del毫无价值。 No values are received on server side.
服务器端未收到任何值。 IE in SaveNew(AssetMgmt del) i get either null or blank object.
在SaveNew(AssetMgmt del)中的 IE我得到null或空白对象。 What i want is value for del that is sent from client side.
我想要的是从客户端发送的del值。
you need to spe4cify the content type as JSON 您需要将内容类型指定为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 这是json函数
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. 现在这是完美的工作,您可以添加更多字段(例如“ AccountId”)并传递给控制器。
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 首先,您在javascript函数中采用数组,例如
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.