1

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>

1 Answer 1

5

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Better use $("#HasChildren").change(function() to avoid additional checking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.