Click here to Skip to main content
15,886,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i can see in the debugger that data is stored in the arrays but when i am sending data to controller the obj is null can someone suggests where am i going wrong ?

What I have tried:

C#
     my model: 

        public class Hello
    {
        public List<string> name;
        public List<string> phone;
        public List<string> contact;
    
    }

my controller code is 
        
      public ActionResult Home(Hello obj) // obj is coming out to be null
        {
                 
        }


my script is 

         var names =[];
         var phones =[];
         var contacts = [];
           
         // some code to fill the arrays 

        var obj = JSON.stringify({
                name: names,
                phone: phones,
                contact: contacts,
              });
            debugger;
            $.ajax({
                cache: false,
                url: 'Home',
                data: { obj:obj },
                success: function (data) {
                    var response = JSON.parse(data);
                    window.location = 'Download?fileGuid=' + response.FileGuid
                                      + '&filename=' + response.FileName;
                }
            })
Posted
Updated 15-Jul-16 3:54am

try this

C#
public ActionResult Home(string obj)
     {
         JavaScriptSerializer js = new JavaScriptSerializer();
         Hello objHello = js.Deserialize<Hello>(obj);
     }


add this reference
C#
using System.Web.Script.Serialization 
 
Share this answer
 
Try moving the JSON.stringify call:
JavaScript
var obj = {
    name: names,
    phone: phones,
    contact: contacts,
});

$.ajax({
    cache: false,
    url: 'Home',
    data: JSON.stringify({ obj:obj }),
...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900