Cancel button in form
Asked Answered
H

9

16

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?

Hustler answered 28/3, 2012 at 18:14 Comment(0)
O
29

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("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.

Osmosis answered 28/3, 2012 at 18:20 Comment(0)
D
7

Lot of the answers worked in either of the browsers, chrome or ie but not all.

This worked in all -

<input type="button" value="Cancel" onclick="location.href='@Url.Action("Index","Home")';"/>
Dockhand answered 8/7, 2014 at 2:7 Comment(0)
P
4

This is my button 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 onclick attribute in some views I do this:

<script>

    $(document).ready(function ()
    {
        $("#cancel").
            attr("onClick",
            "document.location.href='@Html.Raw(Url.Action("Index", "Standard",
             new { ManualId = Model.ManualId, ChapterId = Model.ChapterId }))'");
    });

</script>
Paludal answered 30/6, 2013 at 8:10 Comment(3)
What is Localization.Cancel ?Chandelier
Note that window.history.back() won't work if the form is reloaded (postback). It will just take you back to the earlier version of the form.Chandelier
@MatthewLock it's a localization string that comes from a .resx file. About the reloading, you're absolutely right... it does not work in all cases, but it is ok.Paludal
S
3

Or a styled submit button:

<input type="submit" value="Save Form" name="Save" class="submit-button btn-form" /> 

Then Javascript for cancel button:

<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
Storyteller answered 5/7, 2012 at 16:17 Comment(0)
S
2

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.

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".

The one thing I would add is 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.

Sarver answered 28/3, 2012 at 18:32 Comment(1)
Thank you! I focused on the answer and forgot the rest (Url.Content).Barker
S
2
<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.

Squeegee answered 28/3, 2012 at 18:51 Comment(0)
B
1
<a href="@Url.Action("CancelBid", "Auction")"><img src="../../Content/css/img/btn-submit.png" class="btn-form" /></a>
Barker answered 28/3, 2012 at 18:26 Comment(0)
C
0

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.

@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 form too and only show the confirm dialog if the form had been changed.

Chandelier answered 19/12, 2016 at 5:26 Comment(0)
B
-1
     <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");
     }
Bucky answered 30/7, 2018 at 6:28 Comment(1)
This is web forms, not MVCDyanna

© 2022 - 2024 — McMap. All rights reserved.