asp.net mvc - How to pass a ko.observablearray via JSON to an MVC controller -
i'm using knockout js build model pass mvc controller. ko.observable() items passed controller no problem, however, ko.observablearray([]) data appearing "count=0" @ controller.
below object building in view:
var addviewmodel = function () { self.modelrequest = { object: { vararray: ko.observablearray([]), var1: ko.observable(""), var2: ko.observable("") } }; ....
the modelrequest.object.vararray ko.observablearray contains few attributes in object: name, id, code, type.
below how i'm sending data via json:
p = ko.tojson(addviewmodel.modelrequest); debugger; $.ajax({ url: url, type: 'post', contenttype: 'application/json; charset=utf-8', data: ko.tojson(addviewmodel.modelrequest), success: function (data) { ...something... } });
when debugging code, examine p variable described above , see below:
{"object":{"vararray":[{"name":"name 1", "id":2, "code":"50.1", "type":"a"}], "var1":"abc", "var2":"def"}}
when examine object being passed controller, var1 , var2 have correct values, however, vararray "count=0".
any thoughts? taking time @ this. i'll try ideas @ point!!
edit 10/6/13: controller action:
[httppost] public crudresponse additem(addrequest modelrequest) { ... here ... }
at point when examine modelrequest see vararray "count = 0".
edit 10/8/13: details of addrequest:
#region public members public objecttype object { get; set; } #endregion public members
where objecttype is:
#region public members public int var1 { get; set; } public int var2 { get; set; } public list<spectype> vararray { get; set; } #endregion public members
where spectype
public string name { get; set; } public int id { get; set; } public string code { get; set; } public fieldtype type { get; protected set; }
and fieldtype enum.
update: had found problem. looks property not getting serialized through json when make call web api ui. above-mentioned property of type typaa inherits typeb. typeb contains of fields needed typea. when change property failing serialize of type typeb, instead of typea, serializes fine , of values need reflected in web api.
so, basically, json fails serialize value if it's type derived type. removing inheritance declaring value of base type fixes issue.
so, there way serialize property type inherits class?
eric
i think problem either a: never populating observablearray, or b: not receiving proper object type on controller, either because sending incorrectly or receiving improperly.
try doing -
function testdata(name) { var self = this; self.name = ko.observable(name); }
inside of view model
var addviewmodel = function () { self.modelrequest = { object: { vararray: ko.observablearray([ new testdata('your boy blue'), new testdata('frank tank') ]), var1: ko.observable(""), var2: ko.observable("") } }; }
and see if controller action getting data back.
if not not matching object sending controller object controller recognizes.
Comments
Post a Comment