I want to call the actionlink using jquery , below is the code:
$("#paycheck").click(function () {
if ($("#terms").attr("checked")) {
//Call Html.ActionLink // This is where the html.ActionLink should be called to display another view
} else {
alert("Please agree to the terms and conditions.");
return false;
}
});
<%: Html.ActionLink("Pay", "Index", "News") %>
You don't call an actionlink using jQuery. You could send an AJAX request to the controller action this link is pointing to. If this is what you want, here's how to achieve it:
$(function() {
$('#paycheck').click(function () {
if ($('#terms').is(':checked')) {
// Send an AJAX request to the controller action this link is pointing to
$.ajax({
url: this.href,
type: 'GET',
// you can send some additional data along with the request
data: { foo: 'bar' },
success: function(result) {
// TODO: process the results returned by the controller
}
});
} else {
alert('Please agree to the terms and conditions.');
}
return false;
});
});
Also make sure you provide a correct id (paycheck
) to your action link when generating it
<%= Html.ActionLink("Pay", "Index", "News", null, new { id = "paycheck" }) %>
But if it is only a matter of checking whether the user accepted the terms and conditions and then perform a standard redirect to the controller action without any AJAX, simply do this:
$(function() {
$('#paycheck').click(function () {
if ($('#terms').is(':checked')) {
// by returning true you are letting the browser redirect to the link
return true;
}
alert('Please agree to the terms and conditions.');
// By returning false you stay on the same page and let the user
// agree with the terms and conditions
return false;
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With