如何从jQuery获取一些数据并用它调用Html.ActionLink

时间:2017-08-15 09:17:37

标签: javascript jquery razor html.actionlink

我隐藏了Html.ActionLink,其中“删除” - 动作名称,“学生” - 控制器名称:

@Html.ActionLink("Delete student", "Delete", "Students", new { id = "" }, new { @id = "DeleteButton", @style = "visibility:hidden;" })

和jQuery代码:

<script type="text/javascript">
    $(document).ready(function () {
        var table = $('#students_table').DataTable();

        $('#btnDelete').click(function () {
            var studentId = table.row('.selected')[0]; //this give me correct id
            //alert(studentId); 
            if (studentId) {
                var href = "?id=" + studentId;
                //$("#DeleteButton").attr(href).click();
                //$('#DeleteButton').attr("?id=" + encodeURIComponent(studentId)).click();
            }
            table.row('.selected').remove().draw(false);
        });

    });
</script>

我在调用带有Id的动作链接时出现问题($("#DeleteButton").attr(href).click();

P.S。 #btnDelete - 这是html中简单按钮的id

1 个答案:

答案 0 :(得分:1)

使用ajax执行删除:

<script type="text/javascript">
    $(document).ready(function () {
        var table = $('#students_table').DataTable();

        $('#DeleteButton').click(function (event) {
            event.preventDefault();

            var studentId = table.row('.selected')[0]; //this give me correct id
            //alert(studentId); 
            if (studentId) {
                $.get( "URL_TO_BACKEND?id=" + studentId, function() {
                    table.row('.selected').remove().draw(false);
                });
            }
        });
    });
</script>
  

http://api.jquery.com/jQuery.get/