1

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.

2 Answers 2

2

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" })
Sign up to request clarification or add additional context in comments.

Comments

1

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
    });

1 Comment

Just FYI, this would create a parameter called needtoconfirm, not a onclick param setting the value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.