1110
1110

Reputation: 6829

How to make ajax postback in form with list of check boxes

I dynamically draw checkboxes in my form:

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { id="itemsList"}))
{
foreach (var lt in Model.MyList)
            {                
                <li>                
                    <label id="label">
                        <input value="@lt.itemId" type="checkbox" />
                        @lt.Title</label>
                </li>                        
            } 
}

JQuery function:

$(document).ready(function () {
        $('#itemsList').ajaxForm({
            success: Saved,
            error: HandleError
        });
    });
...

But my action is not fired. Am I doing something wrong here? I am expecting that when I check checkbox make server call.

Upvotes: 0

Views: 615

Answers (2)

archil
archil

Reputation: 39491

I am expecting that when I check checkbox make server call.

You should not expect that unless you've written handler for checkbox change

$(document).ready(function () {
        $('#itemsList').ajaxForm({
            success: Saved,
            error: HandleError
        });

        $(':checkbox').change(function(){
            $('#itemsList').submit();
        });
});

Upvotes: 1

PinnyM
PinnyM

Reputation: 35533

ajaxForm will intercept submissions and send them via ajax. But you need to trigger a submit for the ajax call to kick in.

Try adding:

$('input[@type="checkbox"]').click(function(){ $('#itemsList').submit(); }

You may want to refine the checkbox selector to something more specific...

Upvotes: 0

Related Questions