我想使用按钮而不是 ActionLink 重写下面的代码。任何帮助将不胜感激。谢谢。
@Html.ActionLink("Test Link","Index", "Home", new { id = item.id})
是否可以像下面这样写,或者有更好的方法吗?我也不确定语法,所以请随时更正它。
<input type="button" value="Submit" onclick="location.href='@Url.Action("Index", "Home", new { id = @Model.FirstorDefault.Id.ToString()}, null)'" />
最佳答案
改为将其写为 anchor 标记,因为您似乎想在单击按钮时重定向用户。如果您愿意,您可以轻松地将锚定样式设置为看起来像按钮。
<a href="@Url.Action("Index", "Home",
new { id = Model.FirstorDefault.Id.ToString()})">Submit</a>
现在“提交”标题没有意义,因为您正在执行“GET”操作而不是“POST”。但如果你坚持使用按钮,你可以这样做:
<input type="button" value="Submit" id="btn1" />
// just replace zero with whatever value you need to use
$("#btn1").click(function () {
window.location.replace('@Url.Action("about", "Home", new { id = 0 })');
});
关于html - ASP.NET MVC - Razor - 使用操作、 Controller 和 html 属性重载按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15945469/