【发布时间】:2015-01-22 17:56:30
【问题描述】:
我有一个模型要传递给视图并使用 Html.Raw 将其编码为 JSON 对象:
var model = @Html.Raw(Json.Encode(Model));
在页面上,我从页面中的字段填充模型的各个部分:
model.ProductId = $("#txtProductId").val();
然后尝试使用 ajax 将其发布到控制器:
$.ajax({
type: 'POST',
url: '@Utl.Action("AddProducts"),
data: JSON.stringify(model),
dataType: 'json',
//etc
但它永远不会进入控制器方法:
[HttpPost]
public ActionResult AddProducts(ProductModel, model)
{
//do stuff with the model data
}
谁能在这里帮我解释一下我必须如何更改才能让模型发布?
我的模型,简化:
public class OrderModel {
public ProductModel Product {get;set;}
public PersonModel Person {get;set;}
public List<ProductModel> Products {get;set;}
}
public class ProductModel {
public string Partno {get;set;}
public int Quantity {get;set;}
/// etc
}
public class PersonModel {
public string Surname {get;set;}
public string GivenName {get;set;}
public string Address {get;set;}
/// etc
}
【问题讨论】:
-
你为什么要调用stringify?不需要的
-
好的,我的问题是什么?我尝试只发送模型,但得到了相同的结果,而不是发送到控制器方法。
-
是的,内容类型应该是“application/json; charset=utf-8”
-
@AdrianSalazar contentType 已设置为该值,但仍然失败。还有其他想法吗?
-
你有没有试过发送一个简单的字符串,看看是否能成功?
标签: ajax asp.net-mvc json