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,它只有一个Key,“Name”。
修改
如果我这样做:
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
}
}
答案 0 :(得分:1)
如果您在操作方法的第一行调用UpdateModel(model)
会怎样?可能是因为模型绑定没有隐式地拾取Address属性,你需要给它一个明确的微调。