Как вызвать ссылку html.action с помощью jquery - PullRequest
5 голосов
/ 14 июля 2011

Я хочу вызвать actionlink с помощью jquery, ниже приведен код:

  $("#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") %>

1 Ответ

5 голосов
/ 14 июля 2011

Вы не вызываете actionlink , используя jQuery.Вы можете отправить 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;
    });
});

Также убедитесь, что вы указали правильный идентификатор (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;
    });
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...