BobaFett
BobaFett

Reputation: 33

How do I allow a submit button such as "Delete" to bypass the client-side validation in ASP.NET MVC 3

I have two submit buttons on a page using MVC 3 with client-side validation. There's a text box that is mapped to an object that has data annotations that make it required. As long as I'm trying to click the Save button it works fine. It ensures that the textbox is filled-in.

However, when I try to click the Delete button to go back to the controller and bypass the client-side validation it of course only works if the textbox is filled in. I see this need for situations where there is a Cancel button as well.

I'm unable to find any examples on how to have a submit button that still posts back to the controller but also allow one of the submit buttons to not validate the page.

Here's the markup:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm())
   {

%>
<fieldset>
    <br />
    <div class="grid_10">
        <div class="grid_2 fieldlabel alpha">
            <%= Html.LabelFor(model => model.Name)  %>
        </div>
        <div class="grid_8 omega" style="white-space: nowrap;">
            <%= Html.TextBoxFor(model => model.Name, new { @class = "focus textbox_300" }).MaxLength(100)%>
            <%= Html.ValidationMessageFor(model => model.Name) %>
        </div>
        <div class="clear">
        </div>
        <br />
    </div>
    <div class="clear">
    </div>
    <div class="grid_4 prefix_2">
        <input type="submit" value="Save" id="btnSave" name="btnSubmit" style="width:60px;" />
        &nbsp;&nbsp;
        <input type="submit" value="Delete" id="btnDelete" name="btnSubmit"  style="width:60px;" />
    </div>
    <div class="clear">
    </div>
    <br />
</fieldset>
<% } %>

Upvotes: 2

Views: 923

Answers (1)

hunter
hunter

Reputation: 63562

Are they posting to the same ActionMethod?

You could put it into it's own <form>

Upvotes: 1

Related Questions