c# - How to pass the list of objects from view to MVC controller using json.stringify [solved] -



c# - How to pass the list of objects from view to MVC controller using json.stringify [solved] -

i have tried next code shows null in controller side..

view:

<script> function getme(){ debugger; var emp = [ { empid: 1, name: 'sasi' }, { empid: 2, name: 'sathish'} ]; emp = json.stringify({ 'emp': emp }); $.ajax({ contenttype: 'application/json; charset=utf-8', datatype: 'json', type: 'post', url: '/home/getemplist', data: json.stringify(emp) }); } </script> <input type="button" onclick="getme();" value="click me" />

controller:

public void getemplist(list<emp> emp) { }

emp.cs:

public class emp { public int empid { get; set; } public string name{get;set;} }

please help me solve this..

after lot of searches in google.. found way post list of objects controller.. have modified little bit in code... here is!!

script :

var emp = [{ 'empid': 1, 'name': 'sasi' },{ 'empid': 2, 'name': 'sathish'}]; emp = json.stringify(emp) $.post("/home/getemplist/", { 'emp': emp })

controller:

here changed parameter string type. using javascriptserializer can able convert string info class list objects.. can understand improve if u see code below:

public void getemplist(string emp) { list<emp> persondata; javascriptserializer jss = new javascriptserializer(); persondata = jss.deserialize<list<emp>>(emp); // operation on list object }

include (system.web.script.serialization) namespace in controller inorder utilize javascriptserializer class.

hope helps !! happy coding :)

c# javascript jquery asp.net-mvc

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -