我想使用jquery调用actionlink,下面是代码:
$("#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") %>
答案 0 :(得分:5)
你没有使用jQuery 调用actionlink 。您可以向此链接指向的控制器操作发送AJAX请求。如果这是你想要的,这里是如何实现的 它:
$(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;
});
});
另外,请确保在生成时为您的操作链接提供正确的ID(paycheck
)
<%= Html.ActionLink("Pay", "Index", "News", null, new { id = "paycheck" }) %>
但是,如果只是检查用户是否接受条款和条件,然后在没有任何AJAX的情况下执行标准重定向到控制器操作,只需执行以下操作:
$(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;
});
});