Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone,

I have some variables in a javascript program. The variables are as follow :

C#
var name = "Kishan";
var marks = 76;
var subjects = new Array();
subjects[0] = new Array("S1","S2", "S3");
subjects[1] = new Array("S4","S5", "S6");


I have a model class in C#

C#
public class Student
{
   public string Name {get; set;}
   public double Marks {get; set;}
   public List<string[]> Subjects {get; set;}
}


I have an action method in MVC 4 controller class

C#
[HttpPost]
public ActionResult(Student model)
{
  return View();
}

I have written the code in jquery to post the data from view to action

C#
var source = {
                'Name': name,
                'Marks': marks,
                'Subjects': subjects
            }

            $.ajax({
                type: "POST",
                dataType: "json",
                url: "/Admin/Create",
                data: source,
                success: function (data) {
                    alert("Record added successfully.");
                },
                error: function (error) {
                    jsonValue = jQuery.parseJSON(error.responseText);
                    alert("Error : " + jsonValue);
                }
            });



Now the problem is that I can see data in name and marks variables but the array (subjects) in showing null.

I have to pass this array to controller.

Please help.

Thanks in advanced :)
Posted
Updated 14-Sep-19 17:10pm
v2

C#
var name = "Kishan";
var marks = 76;
var employees = [
{ "firstName":"John" , "lastName":"Doe" }, 
{ "firstName":"Anna" , "lastName":"Smith" }, 
{ "firstName":"Peter" , "lastName": "Jones" }



  var data1= [{ "fname": "Danny", "lname": "LaRusso", "phones":employees}];
            $.ajax({
        url:'@Url.Action("test", "controller")',
            data: JSON.stringify(data1),
            type: 'POST',
            contentType: 'application/json;',
            dataType: 'json',
            success: function (result) {
                //  alert(result.ItemList[0].Str);
            }
        });



In model 
public class Person
    {
        public string fname{ get; set; }
        public string lname{ get; set; }
        public List<phone> phones { get; set; }
    }

    public class Phone
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
    }

and then controller

 public ActionResult test(List<person> person)
        {
           
            //return Json(data,JsonRequestBehavior.AllowGet);
            return View(person);
        }

it should work fine.
tell if it is not work</person></phone>
 
Share this answer
 
v3
Comments
Member 11420653 20-Jun-16 11:06am    
hi Mr.Murali Vijay
In 'Solution2' i think We want Pass a Person Whoes Have a phone collecions To Controller.

public ActionResult test(List<person> person)

But Whay You Give a List Of Persons? I can't Understood.
And With This Solution Can I Show My Collection Item Whoes I Pass To The Controller By Json, In Table? I mean I Read Person Model And Set to table For Display My Recently Added Phones?
Thank You.
You need to set traditional:true in ajax settings

like this
$.ajax({
//Stuff...
traditional:true
});
 
Share this answer
 
Comments
Kishan Gupta 5-Jun-14 7:43am    
Thanks for reply. but it's not working, it's making all data null.
Murali Vijay 5-Jun-14 7:58am    
$.ajax({
//Stuff...
traditional:true,
data: JSON.stringify({ data: values }


});

In controller

public ActionResult test(List<string> data)
{

//return Json(data,JsonRequestBehavior.AllowGet);
return View(data);
}

try this
Murali Vijay 5-Jun-14 8:12am    
Also, don't forget to stringify your json before sending it.
var name = "Kishan";
var marks = 76;
var subjects = [{"13456"} , {"789465"}}

  var data= { "Name": name , "Marks": marks , "Subjects":subjects};
  $.ajax({
        url: "/Student/SaveStudent",
        data: JSON.stringify(data),
        type: 'POST',
        contentType: 'application/json;',
        dataType: 'json',
        success: function (result) {
           console.log(result);
         }
        });
// ViewModel
public class Student
{
   public string Name {get; set;}
   public double Marks {get; set;}
   public List<string[]> Subjects {get; set;}
}
// Student Controler
[HttpPost]
 public ActionResult SaveStudent(Student student)
{           
   return Json(student),JsonRequestBehavior.AllowGet);
}
 
Share this answer
 
v2

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