asp.net-mvc 如何在MVC 4中的按钮onclick事件中调用方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15338524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call method in button onclick event in MVC 4?
提问by Nil
I am developing a MVC application.
我正在开发一个 MVC 应用程序。
In Edit View, I want to have save/submit button and cancel button.
在编辑视图中,我想要保存/提交按钮和取消按钮。
By default MVC given 'back to list' link.this link calls index() method.
默认情况下,MVC 给定“返回列表”链接。此链接调用 index() 方法。
Now, I want to call this index() method on Cancel button's Onclick event , how to do this ?
现在,我想在取消按钮的 Onclick 事件上调用这个 index() 方法,该怎么做?
<input type="submit" value="Save" onclick="return validate();" id="btnSubmit" />
<input type="Button" value ="Cancel" onclick="Index()"style=" font-size:"1.2em"/>
What changes I have to make in above code?
我必须在上面的代码中进行哪些更改?
回答by Darin Dimitrov
You could use the window.location.hrefto redirect to a specified url:
您可以使用window.location.href重定向到指定的网址:
<input type="Button" value="Cancel" onclick="window.location.href='@Url.Action("index")'" style=" font-size:"1.2em"/>
or simply use an anchor instead of a button:
或者简单地使用锚点而不是按钮:
@Html.ActionLink("Cancel", "Index")

