2
Ajax.ActionLink("Link name",....)

it is possible to put checkbox in place of "Link name" ?

if so how?

thanks,

Victor
  • 235
  • 1
  • 5
  • 14

1 Answers1

3

Yes, of course that it is possible. You could use a standard checkbox:

@Html.CheckBoxFor(
    x => x.Foo, 
    new { 
        data_url = Url.Action("SomeAction", "SomeController"), 
        id = "mycheckbox" 
    }
)

and then in your separate javascript file use jQuery to subscribe to the change event of this checkbox and unobtrusively AJAXify it:

$(function() {
    $('#mycheckbox').change(function() {
        var data = {};
        data[$(this).attr('name')] = $(this).is(':checked');

        $.ajax({
            url: $(this).data('url'),
            type: 'POST',
            data: data,
            success: function(result) {
                // TODO: do something with the result    
            }
        });
    });
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 2
    Yes, it works. You cannot have a property in .NET called `data-url`. The helpers in ASP.NET MVC are intelligent enough to interpret `data_url` as the `data-url` attribute when generating the markup. It's a convention. – Darin Dimitrov May 30 '13 at 08:15