尝试将复杂的JSON对象传递给MVC 3中的操作时,我得到一些奇怪的结果。
在操作参数模型上填充了Locations
,但未填充名称和位置。
如果我执行ko.toJS(testViewModel)
,则名称和位置在那里,但是位置为空???
我正在使用kickout.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")
}
通过jQuery ajax发送:
$.ajax({
url: "/ClaimsAuthority/Home/TestIt",
type: "POST",
data: ko.toJSON(testViewModel),
success: function (data, status, xhr) {
//ko.applyBindings(data);
}
});
MVC Action :
<HttpPost()>
Public Function TestIt(model As TestModel) As ActionResult
Return Json(model)
End Function
楷模:
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
最佳答案
尝试在AJAX请求中将内容类型设置为application/json
:
$.ajax({
url: '/ClaimsAuthority/Home/TestIt',
type: 'POST',
contentType: 'application/json',
data: ko.toJSON(testViewModel),
success: function (data, status, xhr) {
//ko.applyBindings(data);
}
});
关于json - 将复杂的JSON对象传递给MVC 3操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9610214/