我的 ajax 调用在我的 web.api 方法中遇到了麻烦。如果我从 api 和 js 中删除 Guid orderId,调用将发送到控制器,但比萨对象是空的。如果我在 URL 中传递 Guid,它也会到达控制器,但没有披萨。请解释为什么这不起作用或帮助我使它起作用。
JS:
var savePizza = function (orderId, pizza) {
var dataCall = $.ajax(config.savePizzaUrl, {
data: ko.toJSON({ orderId: orderId, pizza: pizza }),
type: "post",
contentType: "application/json"
});
return Q.when(dataCall);
};
网络接口:
[HttpPost]
public RequestReturn<Guid> SavePizza(Guid orderId, Pizza pizza)
{
return PizzaRequests.SavePizza(orderId, pizza);
}
JS 对象:
var pizza = function (data) {
this.Id = data.Id;
this.Size = new size(data.Size);
this.SizeId = data.SizeId;
this.Toppings = $.map(data.Toppings, function(item) {return new topping(item);});
};
var topping = function (data) {
this.Id = data.Id;
this.Name = data.Name;
this.Price = data.Price;
};
var size = function (data) {
this.Id = data.Id;
this.Name = data.Name;
this.Price = data.Price;
};
C# 对象:
public class Pizza
{
public Guid Id { get; set; }
public Guid SizeId { get; set; }
public Size Size { get; set; }
public IEnumerable<Topping> Toppings { get; set; }
}
public class Size
{
public Guid Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
public class Topping
{
public Guid Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
JSON Fiddler 帖子捕获: