How to pass a list of selected objects from javascript in action to MVC?

I have a large data table that contains a checkbox for each row. I need to either pass all the items selected as a comma-delimited list, or, nevertheless, a strongly typed IList for the controller. What is the best way to do this? I know that I can pass the string without any problems, but how can I choose which checkboxes are selected?

Here's what the static code looks like:

@using (@Html.BeginForm("RemoveItems", "ResultList", new { IDList = "1,2,3,4" }, FormMethod.Post))
{
    <input type="submit" value="Remove Items" name="RemoveItems" />
}
0
source share
2 answers

You can do it like this using jQuery.

    $.ajax({
        url: "/MyAction/Array",
        data: JSON.stringify({ model: input }),
        type: "POST",
        contentType: "application/json"
    });

Here "input" is your array, "model" is the name of the action parameter. Make sure you specify contentType as application / json.

0
source

I had a site similar to yours.

.

:

var firstPriorityArray = new Array();

        $("Input:checkbox[name=FirstPriority]:checked").each(function () {

            firstPriorityArray[firstPriorityArray.length] = $(this).val();
            console.log(fp.toString());
        });

.log , :)

JSON.Stringify() ASPX AJAX :

var o = new Object();
    o.primary = firstPriorityArray;

var json = JSON.stringify(o);
        $.ajax({
            url: 'getMeAJAX.aspx',
            type: 'POST',
            data: "json=" + json,
            success: function (response) {
                //On success
            },
            error: function (xhr, ajaxoptions, thrownError) {
                alert(thrownError);
            }
        });

, . , "" "MVC" , ;)

//Gerner

0

All Articles