[英]asp mvc controller receiving null values from ajax
have tried the methods found but still cannot seem to tackle the issue.已尝试找到的方法,但似乎仍无法解决问题。 am passing a list of object via Jquery Aja but it seems that the controller is receiving null values (it is recognizing the list count fine).
我正在通过 Jquery Aja 传递 object 的列表,但似乎 controller 正在接收 null 值(它识别列表计数正常)。
the jquery code is as follows: jquery代码如下:
var xmlids = Array();
for (var i = 0; i < orderId.length; i++) {
var xdata = {
"XmlOid": "" +orderId[i] + "",
"CourierCompany": $("#CourierCompany_"+orderId[i]).val() ,
"CourierService": $("#CourierService_" + orderId[i]).val(),
"StoreId" : storeId
};
xmlids.push(xdata);
}
var data = { invoices: xmlids };
$.ajax({
url: purl,
type: "POST",
dataType: "json",
//contentType: "application/json;",
async: false,
data: data,
The controller param is: controller 参数是:
public JsonResult ProcessSaleOrder(List<XMLInvoiceGeneration> invoices)
the object is as follows: object如下:
public class XMLInvoiceGeneration
{
public Int64 XmlOid { get; set; }
public string CourierCompany { get; set; }
public string CourierService { get; set; }
public Int64? StoreId { get; set; }
}
any help appreciated.任何帮助表示赞赏。
I tried to replicate your problem. 我试图重复您的问题。
I created a new ASP.NET MVC web application, and in the Home controller's Index view, I added the following 我创建了一个新的ASP.NET MVC Web应用程序,并在Home控制器的Index视图中添加了以下内容
<input type="button" id="send" value="Send" />
@section scripts{
<script type="text/javascript">
$(function () {
$("input#send").on("click", function () {
var xmlids = Array();
for (var i = 0; i < 2; i++) {
var xdata = {
"XmlOid": 1,
"CourierCompany": "TempData",
"CourierService": "Temp",
"StoreId": 2
};
xmlids.push(xdata);
}
var data = { invoices: xmlids };
$.ajax({
url: "/Home/ProcessSaleOrder",
type: "POST",
dataType: "json",
async: false,
data: data
});
});
})
</script>
}
I basically created some temp data, but trying to still use your original idea. 我基本上创建了一些临时数据,但仍尝试使用您的原始想法。 My controller looks like this:
我的控制器如下所示:
[HttpPost]
public ActionResult ProcessSaleOrder(List<XMLInvoiceGeneration> invoices)
{
return new EmptyResult();
}
When I click the button, and hit the breakpoint in the controller, the invoices
parameter contains 2 items, both with the values I set in the JavaScript code. 当我单击按钮并点击控制器中的断点时,
invoices
参数包含2个项目,两个项目均具有我在JavaScript代码中设置的值。 I'm not sure why your code isn't working, but maybe you can use mine as a template to help you narrow down what the issue is. 我不确定为什么您的代码无法正常工作,但是也许您可以使用我的模板作为模板来帮助您缩小问题的根源。
Try this uncomment //contentType: "application/json;", 尝试以下取消注释// contentType:“ application / json;”,
And send data with JSON.stringify(data)
并使用
JSON.stringify(data)
发送数据
data: JSON.stringify(data)
PS: Best practice for url : url: "@Url.Action("method","controller")"
PS:网址的最佳做法:
url: "@Url.Action("method","controller")"
I had the same issue on sending a List to the controller. 在向控制器发送列表时,我遇到了同样的问题。 After a lot of effort, I found the reason why it is sending as null.
经过大量的努力,我找到了将其发送为null的原因。 It's the fields with Return Type(Class) that are not added with {get;
带有返回类型(类)的字段未与{get; set;
组; }
}
$.ajax({
url: "/Home/CreateTasks",
data: ko.toJSON({Id: this.Id, tasks: objList }),
type: "POST",
dataType: "json",
processData: true,
contentType: "application/json; charset=utf-8",
success: data => {
}
And my Model Class is: After 我的模型课是:之后
public class TaskFields
{
public string Title { get; set; }
public string Activity { get; set; }
}
public class CreateTaskInput
{
public int Id { get; set; }
public TaskFields TaskProperties { get; set; }
}
Before it was: 在此之前:
public class TaskFields
{
public string Title;
public string Activity;
}
public class CreateTaskInput
{
public int Id;
public TaskFields TaskProperties;
}
And My Controller Method is: 我的控制器方法是:
[HttpPost]
public JsonResult MethodName(int Id, List<CreateTaskInput> tasks)
{
}
I am not sure this answer will help you. 我不确定该答案是否会对您有所帮助。 But it may help someone who has a similar mistake.
但这可能会帮助犯过类似错误的人。 Thanks!
谢谢!
Add [FromBody] to the parameters in action将 [FromBody] 添加到参数中
[HttpPost]
public ActionResult ProcessSaleOrder([FromBody] List<XMLInvoiceGeneration> invoices)
{
return new EmptyResult();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.