0

I am trying to retrieve multiple selected values from my ListBox in MVC4. Using the code below I can only retrieve the first value and rest of the selected values does not show up in the ActionResult function.

Not sure what's wrong. Would be great if someone can help as I can not find any online posts on this specific issue. Thanks

I have following html, Jquery and c# code:

@Html.ListBoxFor(m=>m.id, Model.Codes)

 $("#ListBoxID").change(function () {
            if ($(this).val() != "") {
                $("#div").load('actionfunction', {
                    data: $('#ListBoxID').val(),
                });
            }
 });

public ActionResult actionFunction(string data)
{
         // code here BUT 
}

1 Answer 1

1

Since you expect a string in your action. Stringify the array of values (multi-select returns an array on doing val()):

ie. Try

  $("#div").load('actionfunction', {
                data: JSON.stringify($('#ListBoxID').val()),
  });

Simplify it to :

$("#ListBoxID").change(function () {
    var value = $(this).val();
    if (value) {
        $("#div").load('http://www.google.com', {
            data: JSON.stringify(value),
        });
    }
});

If you expect an array string[] data in your action then you don't need to stringify.

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.