2

I have tried with following code but shows null in the 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;}
} 

2 Answers 2

5

After a lot of searches in google.. I found the way to post the list of objects to controller.. i have modified little bit in my code... here it is!!

script :

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

Controller:

Here i just changed the parameter to string type. using JavaScriptSerializer you can able to convert this string data to your class list objects.. you can understand it better if u see my code below:

  public void getemplist(string emp) 
    {
        List<Emp> personData;
        JavaScriptSerializer jss = new JavaScriptSerializer();
        personData = jss.Deserialize<List<Emp>>(emp);
        // DO YOUR OPERATION ON LIST OBJECT

    }

Include this (System.Web.Script.Serialization) namespace in your controller inorder to use the JavaScriptSerializer class.

Hope this helps !! Happy coding :)

Sign up to request clarification or add additional context in comments.

Comments

1

There is problem with data that you are sending to server. you don't need to JSON.stringify 2 times.

 function getme() {
    debugger;
    var emp = [
    { empid: 1, name: 'sasi' },
    { empid: 2, name: 'sathish' }
    ];
    emp = JSON.stringify({ emp: emp });//make json string once 
    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '/Home/getemplist',
        data: emp //send it to server
    });
 }

3 Comments

As a matter of fact, you never need JSON.stringify. jQuery will take care of stringying the object itself. Plus, JSON.stringify is not compatible with certain versions of IE. jQuery makes sure everything remains compatible (if you use a 1.x version).
it doesn't seem to work without JSON.stringify. yes it is not compatible with certain versions of IE so in that case we can use polyfill.
Thanks for your response guys..Finally it is working for me. In controller action i have just changed the parameter type to string Eg: public void getemplist(string emp).And then using the javascript serializer class i was able to convert the string data to list objects.. In script i have just changed the way i call the controller action like this $.post("/Home/getemplist/", { 'emp': emp }). i'll post my entire code here on later.. :) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.