2
AJAX call - 

Here countryId is passed as "AU" for example -

$("#Countries").change(function () {
        var countryId = this.value;
        $.ajax({
            url: '/Registers/GetStates',
            type: "POST",
            data: { 'data': countryId },
            success: function (states) {
                $("#States").html(""); // clear before appending new list 
                $.each(states, function (i, state) {
                    $("#States").append(
                        $('<option></option>').val(state.Id).html(state.Name));
                });
            }
        });
    });

After calling the GetStates method 'countryId' is always passed as null in the controller method. Not sure what I'm missing here. I tried JSON.Stringify({ 'data': countryId }) also. Can anyone help me out ?

Controller Action - 

[HttpPost]
public SelectList GetStates(string countryId)
{
     return Helper.GetStates(countryId);
}
0

1 Answer 1

6

The parameter in your action method should be exactly same as the parameter name in the 'data' option of ajax.

In your case it is 'countryId'

[HttpPost]
public SelectList GetStates(string countryId)
{
     return Helper.GetStates(countryId);
}

Change your ajax data option accordingly.

data: { countryId: countryId },

It will work.

Sign up to request clarification or add additional context in comments.

Comments

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.