【发布时间】:2013-02-06 18:11:56
【问题描述】:
public class Address
{
public string Street { get; set;}
}
public class MyModel
{
public string Name { get; set;}
public Address MyAddress { get; set;}
}
public class MyController : Controller
{
[HttpPost]
public JsonResult DoStuff(MyModel model)
{
// model.Name has its value
// model.MyAddress is there, but its .Street is always null
// Do stuff
}
}
这就是我发布到控制器的方式
var data =
{
__RequestVerificationToken: $("input[name=__RequestVerificationToken]").val(),
Name: "Arnold",
MyAddress:
{
Street: "my address"
}
}
$.ajax({
type: 'POST',
url: "/myroute/dostuff", //Yes i should not use the hardcoded url but this is just for show
data: data,
async: false,
success: function (result) {
// ...
},
dataType: 'json',
});
看着提琴手发布正确的数据.. 如果我查看 ModelState,它只有一个键,“名称”。
编辑:
如果我这样做:
public class MyController : Controller
{
[HttpPost]
public JsonResult DoStuff(FormCollection formCollection)
{
// formCollection has all the data..
// so i guess its the binding? :o any ideas how to fix?
// Do stuff
}
}
【问题讨论】:
-
我认为这不应该被标记为 OrchardCms:它似乎纯粹是一个 ASP.NET MVC 模型绑定问题。问题中还缺少:帖子的结构。模型绑定很大程度上依赖于发布数据结构的约定。
标签: asp.net-mvc