gpt4 book ai didi

C# MVC 通过 JQuery AJAX 发送对象

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:00 25 4
gpt4 key购买 nike

尽管我深入研究了 stackoverflow 和大量文档,但我仍然坚持:我无法通过 jquery ajax 将对象发送到 c# mvc 应用程序。我在 Controller 中返回的顺序的属性值总是看起来是空的。

感谢您的帮助!

JavaScript

function Order()
{
this.Partner;
this.OrderID;
this.TypeOfOrder;
this.SubmittedBy;
this.CompanyID;
this.CompanyName;
}

jQuery('button[type="submit"]').click(function (event) {
event.preventDefault();

var order = new Order();
order.Partner = "ABC",
order.OrderID = "123";
order.TypeOfOrder = "Website";

console.log(order);

jQuery.ajax({
url: "/my-route",
type: "POST",
dataType: 'json',
contentType: "application/json",
data: order,
success: function (data) {
console.log(data);
},
failure: function (response) {
console.log("ERROR!");
}
});
})

C# 模型

public class Order
{
public string Partner { get; set; }
public string OrderID { get; set; }
public string TypeOfOrder { get; set; }
public string SubmittedBy { get; set; }
public string CompanyID { get; set; }
public string CompanyName { get; set; }
}

C# Controller

// POST my-route
[HttpPost]
public Order Post(Order order)
{
return order;
}

控制台

enter image description here

最佳答案

您需要更改您的方法签名以包含 [FromBody]

so: public Order Post([FromBody]Order order)

关于C# MVC 通过 JQuery AJAX 发送对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50612773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com