开发者

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

开发者 https://www.devze.com 2023-01-03 21:39 出处:网络
T开发者_开发百科hanks 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

T开发者_开发百科hanks 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.


<input type='button' onclick='javascript:history.go(-1);return false;' />

the javascript go function does not do any postback.

also, if you are using MVC2 i would not use the server controls like 'Button' you risk ending up with unexpected results.

0

精彩评论

暂无评论...
验证码 换一张
取 消