MVC 3,剃刀视图 - 覆盖默认表单帖子
本文关键字:默认 表单 覆盖 剃刀 视图 MVC | 更新日期: 2023-09-27 18:37:25
我遇到了一个小问题。我在已经发布到控制器操作方法的表单中具有此按钮,例如Edit()
. 现在我想要一个取消按钮,它将发布到不同的操作方法,例如Edit(int id)
. 我正在尝试覆盖Html.Begin
表单中指定的默认帖子,所以我附加了一个 Javascript 事件,但该事件没有事件命中。 我知道如果我使用操作链接,它就像一个魅力,但我担心使链接看起来像一个按钮。此外,我不能为了在表单中具有不同元素的物理放置而将链接放入不同的形式中。真的非常感谢任何提示。
@using (Html.BeginForm("Edit", "RunLogEntry", FormMethod.Post, new { enctype = "multipart/form-data" })) {
<input id="create" class="art-button" type="submit" value="Save" />
<input id="cancel" class="art-button" type="submit" value="Cancel" />
}
$("#cancel").click(function(e) {
e.preventDefault(); //Keep form from posting
window.location.href = "REDIRECT URL";
});
将cancel
按钮的类型更改为button
。这样,默认情况下它不会触发表单提交。你需要用JavaScript来处理它。
@using (Html.BeginForm("Edit", "RunLogEntry", FormMethod.Post, new { enctype = "multipart/form-data" })) {
<input id="create" class="art-button" type="submit" value="Save" />
<input id="cancel" class="art-button" type="button" value="Cancel" />
}
此外,在这种情况下,您不需要e.preventDefault();
行。
$("#cancel").click(function(e) {
window.location.href = "REDIRECT URL";
});