jquery - Asp.net MVC deserialize Kendo Grid data in the controller -
i have asp.net mvc 4 application. have view kendo.grid , want send ajax (json.stringify) data of grid @ controller.
my problem controller receives list of string , don't know how deserialize json data.
code
this object contained in rows of grid:
public class coursedto { public string codcourse { get; set; } public string descourse { get; set; } }
this definition of grid in view:
@(html.kendo().grid<coursedto>() .name("gridcourses") .columns(columns => { columns.bound(c => c.codcourse); columns.bound(c => c.descourse); }) .datasource(datasource => datasource .ajax() .serveroperation(false) .read("getallcourses", "controllercourses") ) )
this ajax call controller jquery:
function passthegrid() { $.ajax({ url: "@url.action("showcourses")", type: "post", data: { courses : json.stringify($("#gridcourses").data("kendogrid").datasource.data()); }, success: function (response) { $("#coursesdiv").html(response); } }); }
this controller:
[httppost] public partialviewresult showcourses(list<string> courses) { javascriptserializer js = new javascriptserializer(); list<string> temp= new list<string>(); foreach (var singlecourse in courses) { temp.add(js.deserialize<string>(singlecourse)); } ... return partialview("_viewcourses"); }
in example have cource in grid , when pass data ajax @ controller, parameter "courses" contains string:
[{"codcourse":"privsic0102006","descourse":"multimedia - privacy , security"}]
but error when try deserialize:
type 'system.string' not supported deserialization of matrix
question
how deserialize json data in controller correctly?
i resolved changing code:
call ajax
function passthegrid() { var o = kendo.observable({ mycourses: $("#gridcourses").data("kendogrid").datasource.data() }); var courses = json.stringify(o); $.ajax({ url: "@url.action("showcourses")", type: "post", data: { courses : courses , success: function (response) { $("#coursesdiv").html(response); } }); }
the ajax call pass data:
{"mycourses":[{"codcourse":"privsic0102006","descourse":"multimedia - privacy , security"}]}
the controller
[httppost] public partialviewresult showcourses(string courses) { var coursesjson = jobject.parse(courses).selecttoken("mycourses").tostring(); var courseslist = jsonconvert.deserializeobject<list<coursedto>>(coursesjson); ... return partialview("_viewcourses"); }
and course list contains object coursedto.
Comments
Post a Comment