将复杂JSON对象传递给MVC 3操作

Sam*_*Sam 6 json asp.net-mvc-3

尝试将复杂的JSON对象传递给MVC 3中的操作时,我得到了一些奇怪的结果.

Locations被填充的动作参数模型,但名称和位置都没有.

如果我这样做ko.toJS(testViewModel),那么名称和位置都在那里,但位置是空白的???

我正在使用knockout.js:

var testViewModel = {
        Name: ko.observable("Joe Bob"),
        Locations: ko.observableArray([
            { ID: 1, Name: "Salem, OR" },
            { ID: 2, Name: "Big Bear Lake, CA" },
            { ID: 3, Name: "Big Bear City, CA" }
        ]),
        Position: ko.observable("Manager")
    }
Run Code Online (Sandbox Code Playgroud)

通过jQuery ajax发送它:

$.ajax({
            url: "/ClaimsAuthority/Home/TestIt",
            type: "POST",
            data: ko.toJSON(testViewModel),
            success: function (data, status, xhr) {
                //ko.applyBindings(data);
            }
        });
Run Code Online (Sandbox Code Playgroud)

MVC行动:

<HttpPost()>
        Public Function TestIt(model As TestModel) As ActionResult
            Return Json(model)
        End Function
Run Code Online (Sandbox Code Playgroud)

楷模:

Public Class TestModel 
    Public Property ID As Integer
    Public Property Name As String
    Public Property Locations As ICollection(Of LocationModel)
    Public Property Position As String  
End Class

Public Class LocationModel
    Public Property ID As Integer
    Public Property Name As String
    Public ReadOnly Property DisplayText As String
        Get
            Return String.Format("({0}) {1}", ID, Name)
        End Get
    End Property
End Class
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 15

尝试application/json在AJAX请求中设置内容类型:

$.ajax({
    url: '/ClaimsAuthority/Home/TestIt',
    type: 'POST',
    contentType: 'application/json',
    data: ko.toJSON(testViewModel),
    success: function (data, status, xhr) {
        //ko.applyBindings(data);
    }
});
Run Code Online (Sandbox Code Playgroud)