Ram Singh Ram Singh - 2 years ago 68
Javascript Question

How to pass a List of a class into model of ajax post call MVC?

I have a model class like below:

public class clsUser
public string FirstName{ get; set; }
public String username { get; set; }
public String password { get; set; }
public List<ContactNumbers> Cnumbers{ get; set; }

Now i want to send ajax request with above model. i have tried to do same by using the following code:

var dd =

"FirstName": "ABC",
"username": "abc123",
"password": "abc@123",

var objInsertUser= JSON.stringify(dd);
var URLValue = "http://localhost:47083/api/TestAPI/insertUser";
type: "POST",
url: URLValue,
data: objInsertUser,
contentType: "application/json",
success: function (data) {


But this always sending "Cnumbers" as null. i am not getting why..

If i am doing in wrong way could experts tell me the correct way..

thanks in advance.

Answer Source

You need to create proper JSON. You need to create an array for Cnumbers

var dd = {
    "FirstName": "ABC",
    "username": "abc123",
    "password": "abc@123",
    "Cnumbers": [{
        "Home": "0987654321"
    }, {
        "Company": "7654321"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download