user1195880
user1195880

Reputation: 11

How to trigger an ajax event with a checkbox click in ASP.NET MVC?

I searched the database, and I found someone talking about it but not demonstrating the fact Adding a onclick dynamically using an Html Helper in MVC

I would like to trigger ajax on a check box onclick event

 @Html.CheckBoxFor(model => model.HasChildren, new { onclick = "return Triger2();" })

Can I execute an AJAX trigger, or can someone please direct me to the jQuery method where I would catch the click event. Then how would it execute the AJAX trigger?

          <div id="Stream1"> @Ajax.ActionLink("Click", "Triger2", new AjaxOptions
            {
                UpdateTargetId = "Stream1",
                InsertionMode = InsertionMode.Replace,
                HttpMethod = "Get",
                LoadingElementId = "progress"
            })
         </div>

Upvotes: 1

Views: 11351

Answers (1)

Ananda Sudarshan
Ananda Sudarshan

Reputation: 485

well you can do this

@Html.CheckBoxFor(model => model.HasChildren)

later on have below to capture click of checkbox and call ajax.

$("#HasChildren").click(function(){
 $.ajax({

            url: '@Url.Action("actionmethod", "controllername")',
            type: "POST",
            data: //data,
            dataType: //type of response,
            success: function (data) {
//ur code here
}
error: function(data){

}
});});

}

NOTE : if you wanna call ajax not on click but only if value is checked or not then you must use an if condition in the click event to check the same using $(this).is(:checked)

Upvotes: 5

Related Questions