MVC app cancel button navigation

I have an MVC form which a user can arrive at from two entirely different controllers/views - I have a cancel button on the form that I would like to simply navigate the user back to their previous page, whichever one they came from.

The button is a straight HTML anchor, not an actionlink:
<a class="btn btn-primary cancelNav" href="">Cancel</a>

Open in new window

On the client-side, I am using unobtrusive jQuery to attach an anonymous function to the link:
    $(document).on('click', '.cancelNav', function () {

Open in new window

I have experimented with numerous approaches here, using window.location, window.history and have verified that the correct URL with params is being passed, but all result in:
"System.ArgumentException: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)'"
yet I can click the browser's back button with no issue.

Can someone tell me why?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

the code you posted is not your issue
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>"System.ArgumentException: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)'"

is the previous page an edit page which required a parameter named as "id"? you need to provide that Id as a numerical value.
Sailing_12PirateAuthor Commented:
Turns out the issue is with the code posted - the blank href attribute in the anchor link is causing the page to re-post to the controller with no ID when the link is clicked.

So, instead of using the window.location.assign method, I am now dynamically assigning the href value:

$('.cancelNav').attr('href', document.referrer);

This has solved the issue.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sailing_12PirateAuthor Commented:
Was able to resolve on my own.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.