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を見ると、キー「名前」が1つしかありません。
編集:
私がこれを行う場合:
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
}
}