Asp.net mvc asp.net mvc json反序列化程序不工作
在一个MVC控制器的帖子中,我使用JQuery发送一个JSON对象,该对象有两个属性:Id和Foos。出于某种原因,每个FooModel实例的所有属性都是null/空的 方法如下:Asp.net mvc asp.net mvc json反序列化程序不工作,asp.net-mvc,Asp.net Mvc,在一个MVC控制器的帖子中,我使用JQuery发送一个JSON对象,该对象有两个属性:Id和Foos。出于某种原因,每个FooModel实例的所有属性都是null/空的 方法如下: [HttpPost] public ActionResult EditFoo(int Id, FooModel[] foos) 这是我的fiddler检索到的表单参数,我可以确认数据是否正在传递到服务器。我还使用两个调试器验证了JSON对象是否包含所有正确的值 Id
[HttpPost]
public ActionResult EditFoo(int Id, FooModel[] foos)
这是我的fiddler检索到的表单参数,我可以确认数据是否正在传递到服务器。我还使用两个调试器验证了JSON对象是否包含所有正确的值
Id 17934
Foos[0][Label] My Foo
Foos[0][Bars][0][Label] First Bar
Foos[0][Bars][0][Id] 1
Foos[0][Bars][1][Label] Second Bar
Foos[0][Bars][1][Id] 2
FooModel如下所示:
public class FooModel
{
public string Label { get; set; }
public IList<Bar> Bars { get; set; }
}
Id=17934&Foos%5B0%5D%5BId%5D=1&&Foos%5B0%5D%5BLabel%5D=My+Foo...
<script type="text/javascript">
var Model = <%= Model.ToJson() %>;
</script>
Foos[0].Bars[0].Label
Id 17934
Foos[0].Labe My Foo
Foos[0].Bars[0].Label First Bar
Foos[0].Bars[0].Id 1
Foos[0].Bars[1].Label Second Bar
Foos[0].Bars[1].Id 2
$.ajax({
url: '/foo',
type: 'POST',
data: JSON.stringify({
Id: 17934,
Foos: [
{
Bars: [
{ Label: 'First Bar', Id: 1 },
{ Label: 'Second Bar', Id: 2 }
]
}
]
}),
contentType: 'application/json',
success: function(result) {
alert('success');
}
});
我找不到一种快速的方法来获取JSON本身作为字符串,但这是分配给ajaxArgs.data的变量。Foos有其所有属性:
var data = { Id: Model.Id, Foos: Model.Foos };
编辑2:
我正在使用JavaScriptSerializer创建一个字符串,如下所示:
public static string ToJson(this object obj)
{
var serializer = new JavaScriptSerializer();
var foo = serializer.Serialize(obj);
return foo;
}
然后使模型在视图中对javascript可用,如下所示:
public class FooModel
{
public string Label { get; set; }
public IList<Bar> Bars { get; set; }
}
Id=17934&Foos%5B0%5D%5BId%5D=1&&Foos%5B0%5D%5BLabel%5D=My+Foo...
<script type="text/javascript">
var Model = <%= Model.ToJson() %>;
</script>
Foos[0].Bars[0].Label
Id 17934
Foos[0].Labe My Foo
Foos[0].Bars[0].Label First Bar
Foos[0].Bars[0].Id 1
Foos[0].Bars[1].Label Second Bar
Foos[0].Bars[1].Id 2
$.ajax({
url: '/foo',
type: 'POST',
data: JSON.stringify({
Id: 17934,
Foos: [
{
Bars: [
{ Label: 'First Bar', Id: 1 },
{ Label: 'Second Bar', Id: 2 }
]
}
]
}),
contentType: 'application/json',
success: function(result) {
alert('success');
}
});
你没有正确地使用括号。这:
Foos[0][Bars][0][Label]
应该是这样的:
public class FooModel
{
public string Label { get; set; }
public IList<Bar> Bars { get; set; }
}
Id=17934&Foos%5B0%5D%5BId%5D=1&&Foos%5B0%5D%5BLabel%5D=My+Foo...
<script type="text/javascript">
var Model = <%= Model.ToJson() %>;
</script>
Foos[0].Bars[0].Label
Id 17934
Foos[0].Labe My Foo
Foos[0].Bars[0].Label First Bar
Foos[0].Bars[0].Id 1
Foos[0].Bars[1].Label Second Bar
Foos[0].Bars[1].Id 2
$.ajax({
url: '/foo',
type: 'POST',
data: JSON.stringify({
Id: 17934,
Foos: [
{
Bars: [
{ Label: 'First Bar', Id: 1 },
{ Label: 'Second Bar', Id: 2 }
]
}
]
}),
contentType: 'application/json',
success: function(result) {
alert('success');
}
});
另外,这不是JSON,它们是表单POST参数。问题在于默认的模型绑定器无法理解您所显示的请求。您的请求值应如下所示:
public class FooModel
{
public string Label { get; set; }
public IList<Bar> Bars { get; set; }
}
Id=17934&Foos%5B0%5D%5BId%5D=1&&Foos%5B0%5D%5BLabel%5D=My+Foo...
<script type="text/javascript">
var Model = <%= Model.ToJson() %>;
</script>
Foos[0].Bars[0].Label
Id 17934
Foos[0].Labe My Foo
Foos[0].Bars[0].Label First Bar
Foos[0].Bars[0].Id 1
Foos[0].Bars[1].Label Second Bar
Foos[0].Bars[1].Id 2
$.ajax({
url: '/foo',
type: 'POST',
data: JSON.stringify({
Id: 17934,
Foos: [
{
Bars: [
{ Label: 'First Bar', Id: 1 },
{ Label: 'Second Bar', Id: 2 }
]
}
]
}),
contentType: 'application/json',
success: function(result) {
alert('success');
}
});
或者,如果您使用ASP.NET MVC 3 JSON请求工厂提供程序,则可以发送如下JSON请求:
public class FooModel
{
public string Label { get; set; }
public IList<Bar> Bars { get; set; }
}
Id=17934&Foos%5B0%5D%5BId%5D=1&&Foos%5B0%5D%5BLabel%5D=My+Foo...
<script type="text/javascript">
var Model = <%= Model.ToJson() %>;
</script>
Foos[0].Bars[0].Label
Id 17934
Foos[0].Labe My Foo
Foos[0].Bars[0].Label First Bar
Foos[0].Bars[0].Id 1
Foos[0].Bars[1].Label Second Bar
Foos[0].Bars[1].Id 2
$.ajax({
url: '/foo',
type: 'POST',
data: JSON.stringify({
Id: 17934,
Foos: [
{
Bars: [
{ Label: 'First Bar', Id: 1 },
{ Label: 'Second Bar', Id: 2 }
]
}
]
}),
contentType: 'application/json',
success: function(result) {
alert('success');
}
});
应发送以下请求:
{ Id: 17934, Foos: [ { Bars: [ { Label: 'First Bar', Id: 1 }, { Label: 'Second Bar', Id: 2 } ] } ] }
这就是Fiddler表示形式参数的方式。我去拿一个sample@c_bit,是的,我肯定Fiddler就是这样代表他们的,但也因为他们是这样被送过来的。jQuery允许您使用各种机制通过AJAX发送数据,包括formpost和JSON。很明显,您使用的是form POST变量,而ASP.NET MVC模型绑定器的名称/值对的名称部分格式不正确。不确定我是否理解,该方法是POST。数据是JSON对象。@c_位,否。数据是Javascript对象。当数据使用{name:value}语法通过线路传输时,它才是JSON。这里您看到的是一个通过表单POST参数传输的Javascript对象。感谢您对术语的更正,尽管它没有回答这个问题。我将尝试Darin的JSON valueprovider建议,看看会发生什么。再次感谢。我已经更新了帖子以澄清。。。我不是自己创建表单params的。同样,这就是Fiddler显示它们的方式。@c_位,事实上Fiddler是这样显示参数的,这意味着它们是这样发送的,正如我在回答中所解释的,默认模型绑定器无法理解这样的语法,这解释了为什么会得到null。如果要使用查询字符串参数,我已经展示了所需的正确语法。var data={Id:Model.Id,Foos:Model.Foos};无法处理复杂的参数集合。您可以尝试发送JSON请求,如我的回答所示。为此,您当然需要一个JSON valueprovider。MVC 3已经开箱即用,下面是一个例子,说明了如何在MVC 3的早期版本中发送JSON请求。