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 () {
        window.location.assign(document.referrer);
    });

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?
sailing_12Asked:
Who is Participating?
 
sailing_12Author 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.
0
 
leakim971PluritechnicianCommented:
the code you posted is not your issue
0
 
Ryan ChongCommented:
>>"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.
0
 
sailing_12Author Commented:
Was able to resolve on my own.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.