ASP.Net MVC 2 - How to set up a Cancel button with client side navigation

Posted by arame3333 on Stack Overflow See other posts from Stack Overflow or by arame3333
Published on 2010-06-16T15:47:51Z Indexed on 2010/06/16 15:52 UTC
Read the original article Hit count: 297

Filed under:

Thanks to a previous question I found a useful link on multiple buttons. http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx What I want to do is have a cancel button on my page, similar to this;

            <button name="button" type="button" onclick="document.location.href=$('#cancelUrl').attr('href')">Cancel</button>
 <a id="cancelUrl" href="<%: Url.Action("Index", "Home") %>" style="display:none;"></a>

However although this code works, I really want to go back to the previous page. For Web Forms I could use the javascript Back() or Go(-1) functions, but they relied on postbacks.

I could of course hard code the previous page and controller as I have done above. However I am struggling to find links that explain to me how Url.Action works. Because if I do this, I also need to include an index parameter, and I am not clear how the syntax works for that. It seems odd the amount of coding to do this.

Out of curiosity, I am also wondering how you TDD client side code like this.

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2