1

I am facing a strange problem in my MVC 5 application where I want to pass a Drop Down Selected value to controller using a Ajax post.

the Post code looks like this:

$(function () {
    //Change the Value stream list each time the BU is selected
    $('#Lob').change(function () {   

        alert(JSON.stringify($('#Lob option:selected').text()));

        $.ajax({
            url: '@Url.Content("~/Dashboard/GetValueStreams/")',
            dataType: 'json',
            type: 'POST',
            data: JSON.stringify($('#Lob option:selected').text()),
            contentType: 'application/json',
            success: function (VSList) {
                // do stuff
                });
            }
        });
    });
});

The ALERT works fine and displays the selected value correctly. However in the controller, the string appears as null.

 [HttpPost]
    public ActionResult GetValueStreams(string BUName)
    {   
       // Here the BUName parameter is coming as null.
    }

I have tried changing my JSON POST data to the following:

data: {"BUName": JSON.stringify($('#Lob option:selected').text())},

This also does not work. Any help will be much appreciated. Thanks.

6
  • Without quotes on BUName should work Commented Mar 24, 2019 at 13:15
  • No need for to stringify a string: data: {"BUName": $('#Lob option:selected').text()}, - but you should be getting something in BUName, so there's a problem elsewhere. Commented Mar 24, 2019 at 13:16
  • Why JSON type? You’re passing a string Commented Mar 24, 2019 at 13:16
  • 1
    I think “contentType: JSON” is also out of place Commented Mar 24, 2019 at 13:17
  • Not the problem, but worth mentioning: Use url: '@Url.Action("GetValueStreams", "Dashboard")', Commented Mar 24, 2019 at 13:17

1 Answer 1

2

Change your data to data: JSON.stringify({BUName : $('#Lob option:selected').text()}).

I tested and it worked.

$.ajax({
        url: '@Url.Content("~/Dashboard/GetValueStreams/")',
        dataType: 'json',
        type: 'POST',
        data: JSON.stringify({BUName : $('#Lob option:selected').text()}),
        contentType: 'application/json',
        success: function (VSList) {
            // do stuff
        }
        });
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.