1

I'm passing a list of json data to the controller via a AJAX call. The String arrays "LandPhone, Mobile and Email" are getting null in the controller. Actually there is some values.

Main Model

public class CustomerModel
{
    public string ReferenceId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string TINNo { get; set; }
    public string CurrencyId { get; set; }
    public List<CustomerAddressModel> Address { get; set; }

}

Sub Model

public class CustomerAddressModel
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public int ZipCode { get; set; }
    public string District { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string[] LandPhone { get; set; }
    public string[] Mobile { get; set; }
    public string[] Email { get; set; }

}

AJAX Call

function get() {
$.ajax({
    type: 'POST',
    url: '/temp/Customer',
    data: { "ReferenceId": "", "FirstName": "", "MiddleName": "", "LastName": "", "TINNo": "", "CurrencyId": 0,
            "Address": [{
                "AddressLine1": "", "AddressLine2": "", "AddressLine3": "", "ZipCode": 0, "District": "", "State": "", "Country": "",
                "LandPhone": ["123"],
                "Mobile": ["1234567890", "9876543210"],
                "Email": ["[email protected]", "[email protected]"]
            }]
     },
    dataType: "json",
    //contentType: "application/json;charset=utf-8",
    async: false,
    success: function (data) {
        alert(data);
    }
});

In the Controller Method Data getting like this

In the Controller Method Data getting like this

1 Answer 1

5

You need to include the contentType: "application/json;charset=utf-8", option and also stringify the data

$.ajax({
    type: 'POST',
    url: '@Url.Action("Customer", "temp")', // don't hard code
    contentType: "application/json;charset=utf-8"
    data: JSON.stringify({ "ReferenceId": ..... "Address": [{ "AddressLine1": "" ... }]}),
    ....
Sign up to request clarification or add additional context in comments.

1 Comment

In my case, I also had to add [FromBody] to the controller method. I am using ASP.NET Core 2.0.

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.