自定义jQuery JSON序列化
在我的ASP.MVC中,我有以下模型:自定义jQuery JSON序列化,jquery,asp.net-mvc,Jquery,Asp.net Mvc,在我的ASP.MVC中,我有以下模型: public class MySubModel { public int Id { get; set; } } public class MyModel { public List<MySubModel> Items { get; set; } } 当jQuery发送时 Items[0][Id] 有没有办法定制这个jQuery行为 另外,看起来我可以在客户机上使用JSON.stringify(),在服务器上使用JSON解析器,但这
public class MySubModel
{
public int Id { get; set; }
}
public class MyModel
{
public List<MySubModel> Items { get; set; }
}
当jQuery发送时
Items[0][Id]
有没有办法定制这个jQuery行为
另外,看起来我可以在客户机上使用JSON.stringify(),在服务器上使用JSON解析器,但这对我来说是个难题。有更好的办法吗
另外,看起来我可以在客户端和一些应用程序上使用JSON.stringify()
服务器上的JSON解析器,但这对我来说是一个黑客。有更好的方法吗
路
stringify就足够了,服务器上不需要任何JSON解析器,底层框架已经为您处理了
例如:
var model = { items: [] };
// now add some elements to the items array
model.items.push({ id: 1 });
model.items.push({ id: 2 });
model.items.push({ id: 3 });
...
$.ajax({
url: 'your controller action endpoint comes here',
method: 'post',
contentType: 'application/json',
data: JSON.stringify(model),
success: function(result) {
// do something with the results here
}
});
将成功发送到以下操作:
[HttpPost]
public ActionResult SomeAction(MyModel model)
{
... use your model here directly without any JSON parsing
}
AJAX请求的两个重要参数如下:
contentType: 'application/json',
data: JSON.stringify(model)
当然,model
javascript变量应该反映您的服务器端视图模型结构,如图所示(它应该有一个items
array属性,该属性包含一个id
属性的元素)
另外,看起来我可以在客户端和一些应用程序上使用JSON.stringify()
服务器上的JSON解析器,但这对我来说是一个黑客。有更好的方法吗
路
stringify就足够了,服务器上不需要任何JSON解析器,底层框架已经为您处理了
例如:
var model = { items: [] };
// now add some elements to the items array
model.items.push({ id: 1 });
model.items.push({ id: 2 });
model.items.push({ id: 3 });
...
$.ajax({
url: 'your controller action endpoint comes here',
method: 'post',
contentType: 'application/json',
data: JSON.stringify(model),
success: function(result) {
// do something with the results here
}
});
将成功发送到以下操作:
[HttpPost]
public ActionResult SomeAction(MyModel model)
{
... use your model here directly without any JSON parsing
}
AJAX请求的两个重要参数如下:
contentType: 'application/json',
data: JSON.stringify(model)
当然,
model
javascript变量应该反映您的服务器端视图模型结构,如图所示(它应该有一个items
array属性,其中的元素具有id
属性)。显示客户端代码显示客户端代码