1
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
        }
}
4

1 回答 1

1

如果你UpdateModel(model)在 action 方法的第一行调用会发生什么?这可能是因为模型绑定没有隐式获取 Address 属性,您需要给它一个显式的轻推。

于 2013-02-05T11:06:43.433 回答