hsim
hsim

Reputation: 2080

access to javascript var from actionlink

In my page I have the following script:

<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>

I am able to set the needToConfirm value in a button like this:

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

I want to know if there's a similar way to do this in an actionLink like this one:

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

I've tried this but it doesn't work.

Upvotes: 1

Views: 95

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337560

The third parameter of ActionLink is the route values. You need to put the needtoconfirm in the fourth parameter - htmlAttributes. Try this:

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

Upvotes: 2

Matt Houser
Matt Houser

Reputation: 36053

The version of Html.ActionLink that you are currently using is setting the route values. You want to set the html values.

Try this:

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

Upvotes: 1

Related Questions