Url.Action无法触发ClientController中ShowClient方法的问题求助
问题原因分析
你遇到的问题核心在于表单内的<button>默认类型是submit——点击它时会优先触发表单的提交动作(也就是调用SaveClient方法),完全跳过了你写的onclick跳转逻辑。
解决方案
这里有几种简单的修复方式,任选其一即可:
1. 给按钮指定type="button"
明确告诉浏览器这个按钮只是普通交互按钮,不会触发表单提交:
<button type="button" onclick="location.href='@Url.Action("ShowClient", "Client", new { id=item.ClientCode})'"> <i class="icon-arrow-right"></i> </button>
2. 使用<a>标签替代按钮(更符合语义)
如果这个按钮的作用只是跳转页面,用链接标签更贴合语义,之后可以通过CSS把它样式化成按钮的样子:
<a href='@Url.Action("ShowClient", "Client", new { id=item.ClientCode})' class="button-style"> <i class="icon-arrow-right"></i> </a>
(注:需要给.button-style添加对应的CSS样式,比如背景色、边框、内边距等,模拟按钮外观)
3. 在onclick中阻止默认提交行为
在跳转代码后加上return false;,手动阻止按钮的默认submit行为:
<button onclick="location.href='@Url.Action("ShowClient", "Client", new { id=item.ClientCode})'; return false;"> <i class="icon-arrow-right"></i> </button>
额外说明
根据HTML规范,<button>元素在<form>内部时,默认的type属性值是submit,这意味着它会自动绑定表单的提交事件。所以只要在表单内使用按钮做非提交操作,都需要显式指定type="button"或者手动阻止默认行为。
内容的提问来源于stack exchange,提问作者refresh
相关产品推荐
相关产品推荐

