C# 表单中的取消按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9913353/
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
Cancel button in form
提问by Sachin Kainth
I have a cancel button in a form:
我有一个形式的取消按钮:
@using (Html.BeginForm("ConfirmBid","Auction"))
{
some stuff ...
<input type="image" src="../../Content/css/img/btn-submit.png" class="btn-form" />
<input type="image" src="../../Content/css/img/btn-cancel.png" class="btn-form" />
}
The issue is I want this button to go to a particular view when I click on it. How do I do this?
问题是我希望这个按钮在我点击它时转到一个特定的视图。我该怎么做呢?
采纳答案by Shyju
Either you can convert the Cancel button as an anchor tag with @Html.ActionLink helper method and apply a css class which makes the link to looks like a button and then in the controller action for that link, you can return the specific view.
您可以使用@Html.ActionLink 辅助方法将取消按钮转换为锚标记,并应用一个 css 类,使链接看起来像一个按钮,然后在该链接的控制器操作中,您可以返回特定视图。
@Html.ActionLink("Cancel","Index","Products",null, new { @class="clsButtonFake"})
or
或者
Use 2 submit buttons in the form. One for real submit and one for the cancel. and in your controller action, check which button called the action method. You can read more about it here in this answer.
在表单中使用 2 个提交按钮。一种用于真正提交,一种用于取消。并在您的控制器操作中,检查哪个按钮调用了操作方法。你可以在这个答案中阅读更多关于它的信息。
回答by Adauto
<a href="@Url.Action("CancelBid", "Auction")"><img src="../../Content/css/img/btn-submit.png" class="btn-form" /></a>
回答by mccow002
So with Shyju's appraoch, you use the built in MVC ActionLink helper. Doing this, you'll need to have any images or icons done through css. However, this is much more cachable, especially if you use base64 strings for your images in css.
因此,通过 Shyju 的方法,您可以使用内置的 MVC ActionLink 帮助程序。这样做,您需要通过 css 完成任何图像或图标。但是,这更容易缓存,尤其是当您在 css 中为图像使用 base64 字符串时。
I like Adauto's approach because it gives you much more control of the markup. MVC Html Helpers are nice, but they still seem to have their heads in the WebForms mindset of "don't worry about it, we'll take care of it for you".
我喜欢 Adauto 的方法,因为它可以让您更好地控制标记。MVC Html Helpers 很好,但他们似乎仍然抱着“别担心,我们会为你处理”的 WebForms 心态。
The one thing I would add is Url.Content.
我要添加的一件事是 Url.Content。
<a href="@Url.Action("CancelBid", "Auction")"><img src="@Url.Content("~/Content/css/img/btn-submit.png" class="btn-form" /></a>
It's never really a good idea to make your views have to know the location of content relative to it's location.
让你的视图必须知道内容相对于它的位置的位置从来都不是一个好主意。
回答by Dmitry Efimenko
<a href="/Auction/[ActionName]">
<input type="image" src="@Url.Content("~/Content/css/img/btn-cancel.png")" class="btn-form" />
</a>
if you want to preserve its look as a button, you could do something like this:
如果您想保留其作为按钮的外观,您可以执行以下操作:
<a href="/Auction/[ActionName]">
<input type="button" value="Cancel">
</a>
where [ActionName]is the name of the action that will return your desired view.
[ActionName]将返回所需视图的操作的名称在哪里。
回答by Nathan
Or a styled submit button:
或者样式化的提交按钮:
<input type="submit" value="Save Form" name="Save" class="submit-button btn-form" />
Then Javascript for cancel button:
然后取消按钮的Javascript:
<input type="button" onclick="document.location.href('Home/Index')" value="Cancel" class="cancel-button btn-form" />
// Note: This avoids any of the validation that may happen in the model that
// normally gets triggered with a submit
回答by Leniel Maccaferri
This is my button HTML:
这是我的按钮 HTML:
<button type="button"
class="btn btn-inverse"
id="cancel"
onclick="window.history.back()">
<i class="icon-remove icon-large"></i>
<br />@Localization.Cancel
</button>
Then to customize the onclickattribute in some views I do this:
然后onclick在某些视图中自定义属性,我这样做:
<script>
$(document).ready(function ()
{
$("#cancel").
attr("onClick",
"document.location.href='@Html.Raw(Url.Action("Index", "Standard",
new { ManualId = Model.ManualId, ChapterId = Model.ChapterId }))'");
});
</script>
回答by rmdev
Lot of the answers worked in either of the browsers, chrome or ie but not all.
很多答案在浏览器、chrome 或 ie 中都有效,但不是全部。
This worked in all -
这在所有方面都有效 -
<input type="button" value="Cancel" onclick="location.href='@Url.Action("Index","Home")';"/>
回答by Matthew Lock
I ended up making a helper so I could reuse the cancel button. I added a js confirm in case people click the cancel button by accident after filling in the form.
我最终制作了一个助手,以便我可以重复使用取消按钮。我添加了一个 js 确认,以防人们在填写表单后不小心点击了取消按钮。
@helper FormCancelButton(string cancelUrl)
{
<button type="button" class="btn" onclick="if (confirm('Cancel changes?')) location.href = '@cancelUrl';">Cancel</button>
}
I then call it like so:
然后我这样称呼它:
@FormCancelButton(Url.Action("Index", "User" ))
If you are really keen you could try and detect the dirty state of the formtoo and only show the confirm dialog if the form had been changed.
如果您真的很热衷,您也可以尝试检测表单的脏状态,并且仅在表单已更改时才显示确认对话框。
回答by Sonu Singh
<asp:Button runat="server" class="btn btn-danger"
CausesValidation="false" onclick="Cancel_Click" Text="Cancel"/>
protected void Cancel_Click(object sender, EventArgs e)
{
Response.Redirect("Test.aspx");
}

