从actionlink访问javascript var

时间:2013-09-04 14:18:17

标签: javascript jquery asp.net-mvc razor

在我的页面中,我有以下脚本:

<script language="javascript">
    var needToConfirm = true;

    window.onbeforeunload = confirmExit;

    function confirmExit() {
        if (needToConfirm) {
            return "You have attempted to leave this page. " +
                "If you have made any changes to the fields without clicking the Save button, your changes will be lost." +
                " Do you wish to proceed?";
        }
    }
</script>

我可以在这样的按钮中设置needToConfirm值:

<input type="submit" value="Save" name="_submitButton" onclick="needToConfirm = false"/>

我想知道在这样的actionLink中是否有类似的方法:

@Html.ActionLink("Remove this item", "RemoveItemEdit", new
                            {
                                @_cardID = Model.mPackToEdit.mListCardPack[i].mCard.mMasterCard.mCardID,
                                needToConfirm = false
                            })

我试过这个,但它不起作用。

2 个答案:

答案 0 :(得分:2)

ActionLink的第三个参数是路线值。您需要将needtoconfirm放在第四个参数 - htmlAttributes中。试试这个:

@Html.ActionLink(
    "Remove this item", 
    "RemoveItemEdit", 
    new { @_cardID = Model.mPackToEdit.mListCardPack[i].mCard.mMasterCard.mCardID },
    new { "onclick" = "needToConfirm = false" })

答案 1 :(得分:1)

您当前使用的Html.ActionLink版本是设置路线值。您想设置 html值

试试这个:

@Html.ActionLink("Remove this item", "RemoveItemEdit", 
    new { 
        _cardID = Model.mPackToEdit.mListCardPack[i].mCard.mMasterCard.mCardID
    },
    new { 
        needToConfirm = false
    });