Jony shah
Jony shah

Reputation: 21

Pass List from view to controller

i am trying to pass List from view to controller but i am getting null value. code is written as follows :

model class :

public class testModel
{
    public int ID { get; set; }

    public string Name { get; set; }

    public List<myModel> ParameterList {get;set;}
}

jquery and ajax code to post data to controller :

var myModel = {
                  "Name":"test",      
                  "Description":"desc"      
                };

        var Object = {
            Name: $("#Name").val(),
            ParameterList : myModel
        };

        $.ajax({
            url: '/controller/action',
            type: 'POST',
            data: JSON.stringify(Object),
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {   }
                else { }
            }
        });

i am getting value for Name property but not for ParameterList. What is wrong here ? am i missing anything ?

Please help me out.

Thanks

Edit : Controller Code from comments,

 public JsonResult Save(Object Obj) 
 {
 // logic for managing model and db save
 } 

Upvotes: 1

Views: 2711

Answers (3)

sasi
sasi

Reputation: 81

Just try like this..Hope this would help!!

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

}

Upvotes: 0

karaxuna
karaxuna

Reputation: 26930

Try this:

var myModel = [{
              "Name":"test",      
              "Description":"desc"      
            }];

    var Object = {
        Name: $("#Name").val(),
        ParameterList : myModel
    };

    $.ajax({
        url: '/controller/action',
        type: 'POST',
        data: Object,
        dataType: 'json',
        traditional: true,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {   }
            else { }
        }
    });

Upvotes: 1

Yasser Shaikh
Yasser Shaikh

Reputation: 47784

You said,

I am getting value for Name property but not for ParameterList.

Which makes me wonder what is the structure of myModel, as you have declared ParameterList as a list of myModel type : List<myModel> ParameterList

Also I would recommend you to log JSON.stringify(Object) to console and check the Json value you are actually posting to the controller.

Here is what I found you must be posting back

{"Name":"yasser","ParameterList":{"Name":"test","Description":"desc"}} 

Also read these articles :

How can I pass complex json object view to controller in ASP.net MVC

Pass JSON object from Javascript code to MVC controller

Upvotes: 3

Related Questions