6
我收到一些奇怪的結果,而試圖將複雜的JSON對象傳遞給一個動作在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")
}
通過jQuery AJAX發送它:
$.ajax({
url: "/ClaimsAuthority/Home/TestIt",
type: "POST",
data: ko.toJSON(testViewModel),
success: function (data, status, xhr) {
//ko.applyBindings(data);
}
});
MVC操作:
<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
感謝達林!這工作!很快我會再給你一杯啤酒! – Sam 2012-03-07 22:47:11
是否有可能以這種方式分析複雜模型並將它們作爲輸入對象接收到控制器操作中?因爲我試圖做同樣的事情,但收集的對象,雖然與實例,來與空屬性值。 – 2013-08-16 23:03:33
@GustavoRubio,是的,可以使用任意複雜的對象和集合。只有循環對象引用不受支持,因爲它們不能被JSON序列化。 – 2013-08-18 17:57:16