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,它只有一个键,"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
}
}发布于 2013-02-05 11:06:43
如果在操作方法的第一行调用UpdateModel(model),会发生什么情况?这可能是因为模型绑定没有隐式地获取Address属性,您需要给它一个明确的提示。
https://stackoverflow.com/questions/14703139
复制相似问题