Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse jQuery serialized data in C#

I have a ListBox on my page. I'm making an AJAX call to a C# function, and I need to pass the values of the selected items. Here's what I have:

$('#btnSubmit').click(function() {
    $.ajax({
        type: "POST",
        url: 'Default.aspx/GetSelectedValues',
        data: '{selectedValues: ' + $('#lbItems').serialize() + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess
    });
});

<select id="lbItems" multiple="multiple">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
    <option value="5">Five</option>
</select>


[System.Web.Services.WebMethod]
public static string GetSelectedValues(string selectedValues)
{
    //
}

The data being passed to the C# function looks like:

lbItems=1&lbItems=3&lbItems=5

Is there any built-in C# function that can deserialize that easily, to convert the values into an array of some sort? Or maybe there's a better way to pass the data from jQuery?

like image 286
Steven Avatar asked May 24 '26 21:05

Steven


1 Answers

System.Web.HttpUtility.ParseQueryString

like image 188
Rei Miyasaka Avatar answered May 26 '26 11:05

Rei Miyasaka