通过jQuery AJAX将对象数组传递给控制器,动作数据在ASP.NET Core 2.2 MVC中始终为空-它与.NET Framework 4.5一起使用 [英] Pass array of objects to controller by jQuery AJAX, action data is always null in ASP.NET Core 2.2 MVC - it is working with .NET Framework 4.5
问题描述
我在一个项目中工作,我需要使用ajax调用来保存页面中包含订单详细信息的对象数据.当我将项目与.NET Framework 4.5版一起使用时,它可以很好地工作-但是,当我使用ASP.NET Core MVC时,它不能工作.有什么帮助吗?
I am working in project that I need to use ajax call to save object data from my page witch contain order details. When I use project with .NET framework version 4.5, it is working very well - but when I use ASP.NET Core MVC, it is not working. Any help please?
我确实尝试过类似问题中的每件事,但是它们没有起作用.例如,我使用符号[frombody]
-但我仍然遇到相同的问题.
I did try every thing in similar questions but they did not work. For example I use notation [frombody]
- but I still have the same problem.
我有以下控制器操作:
public ActionResult CreateOrders([FromBody] T_POS_ENT_ORDER_DETIALS_Temp []T_POS_ENT_ORDER_Data)
{
GRepository<T_POS_ENT_ORDER_DETIALS_Temp> t_pos_ent_orderOpr = new GRepository<T T_POS_ENT_ORDER_DETIALS_Temp>();
T_POS_ENT_ORDER_Data.order_Date = DateTime.Now;
t_pos_ent_orderOpr.Add(T_POS_ENT_ORDER_Data);
return Json(new { msg = "Successfully added " });
}
我有以下ajax调用:
And I have the following ajax call:
var DATA = [];
DATA.push({ LocPrice: "12" });
DATA.push({ LocProductID: "1002" });
DATA.push({ discount: "0" });
DATA.push({ posNumber: "1" });
DATA.push({ productName: "soap" });
DATA.push({ productQynt: "1" });
$.ajax({
url: '/AjaxT_POS_ENT_ORDER/CreateOrders',
data: JSON.stringify({ 'billArray': DATA }),
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
async: false,
success: OnSuccess,
});
我也有这个课程:
public class T_POS_ENT_ORDER_DETIALS_Temp
{
public string LocPrice { get; set; }
public string LocProductID { get; set; }
public string discount { get; set; }
public string posNumber { get; set; }
public string productName { get; set; }
public int productQynt { get; set; }
}
没有错误显示仅订单明细始终为空...
There is no error showing only the order details is always null ...
推荐答案
我正在.Net Core 3.1中工作,这对我有用:
I'm working in .Net Core 3.1 and this works for me:
1.控制器
尝试使用 [FromForm] ,而不是 [FromBody]
[HttpPost]
public ActionResult CreateOrdersPOSTObject([FromForm] List<T_POS_ENT_ORDER_DETIALS_Temp> prm)
{
//your logic...
return Json(new { msg = "Successfully added " });
}
2. AJAX通话
不应使用 JSON.stringfy()或 contentType .
var DATA = [];
DATA.push({ LocPrice: "12", LocProductID: "1002", discount: "0", posNumber: "1", productName: "soap", productQynt: "1" });
DATA.push({ LocPrice: "23", LocProductID: "1003", discount: "0", posNumber: "1", productName: "shampoo", productQynt: "1" });
DATA.push({ LocPrice: "7",LocProductID: "1004" ,discount: "0",posNumber: "2" ,productName: "sponge",productQynt: "2"});
$.ajax({
url: '@Url.Content("~/home/CreateOrdersPOSTObject")',
data: { "prm": DATA },
type: "POST",
dataType: "json",
async: true
});
或者,如果您愿意,也可以使用速记 $.post 获得相同的结果
Or if you prefer, you can use the shorthand $.post with the same results
var DATA = [];
DATA.push({ LocPrice: "12", LocProductID: "1002", discount: "0", posNumber: "1", productName: "soap", productQynt: "1" });
DATA.push({ LocPrice: "23", LocProductID: "1003", discount: "0", posNumber: "1", productName: "shampoo", productQynt: "1" });
DATA.push({ LocPrice: "7",LocProductID: "1004" ,discount: "0",posNumber: "2" ,productName: "sponge",productQynt: "2"});
$.post(
'@Url.Content("~/home/CreateOrdersPOSTObject")',
{ "prm": DATA },
function () {
alert( "success" );
})
.fail(function() {
alert( "error" );
});
这篇关于通过jQuery AJAX将对象数组传递给控制器,动作数据在ASP.NET Core 2.2 MVC中始终为空-它与.NET Framework 4.5一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!