2

I have 2 buttons on my form, a Submit and Save button. I want to be able to skip all validation when a user clicks the Save button. I've tried adding class="cancel", formnovalidate, formnovalidate="formnovalidate", disableValidation="true" to the Save button but none of them worked. Any help would be wonderful! I'm using ASP.NET Core 3.1.

Form

<form asp-action="Request">
...
...
<input name="answer" type="submit" value="Submit" class="btn btn-primary" />
<input name="answer" type="submit" value="Save" class="btn btn-secondary" />
</form>
<partial name="_ValidationScriptsPartial" />
1
  • 1
    formnovalidate attribute only has to do with HTML5 validation, nothing to do with this plugin. Try type="button" instead of type="submit" if you want a button that does not trigger validation. Commented Feb 28, 2020 at 2:56

2 Answers 2

4

Firstly,you need to know,if you use _ValidationScriptsPartial,it would generate the html like below:

Generate cshtml from:

<input asp-for="MyDate" class="form-control" />

to:

<input class="form-control" type="date" data-val="true"
    data-val-required="The MyDate field is required."
    id="MyDate" name="MyDate" value="">

formnovalidate and class="cancel" attribute could skip the validation in client side,but could not skip validation in server side.

To fix such issue,you could clear model state:

1.Model:

public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
}

2.View(add formnovalidate to save input):

@model Test
<form asp-action="Request">
    <div>
        Name:<input asp-for="Name" />
        <span asp-validation-for="Name" class="text-danger"></span>
    </div>
    <div>
        Id:<input asp-for="Id" />
        <span asp-validation-for="Id" class="text-danger"></span>
    </div>
    <input name="answer" type="submit" value="Submit" class="btn btn-primary" />
    <input name="answer" type="submit" value="Save" formnovalidate class="btn btn-secondary" />
</form>
@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

3.Action:

[HttpPost]
public IActionResult Request(Test test)
{
    if (!ModelState.IsValid)
    {
         ModelState.Clear();
         return View("Index");
    }
    return View("Index");
}

4.Result: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This solved the problem! I also had an internal RCL that was running validation that I didn't realize. Thanks!
0
<form asp-action="Request">
...
...
<input name="answer" type="submit" value="Submit" class="btn btn-primary" />
<input name="answer" type="submit" value="Save" class="btn btn-secondary cancel" />
</form>

@section Scripts{
<partial name="_ValidationScriptsPartial" />
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.