badraih alshehri
badraih alshehri

Reputation: 19

ASP.NET Core disable validation when button is pressed

On the razor page, I have two buttons of type 'submit'.

The first button is used to add data to a local table, and the second button is used to add all columns in the database.

I want to prevent the first button from checking the validation of fields, only the second button checks the fields.

How to prevent the first button from checking the validation of the fields?

(ASP.NET Core)

Upvotes: 1

Views: 1446

Answers (3)

Divakar
Divakar

Reputation: 1

You can create those two buttons like below.

The post request will be sent when the type="submit" button is triggered. In the other case, the get request can be executed and you can write your code inside the get related action method.

<button type="submit" class="btn btn-success" asp-action="Create" asp-controller="Category">Submit</button>
<a class="btn btn-success" asp-controller="Category" asp-action="Index" >Back</a>

Upvotes: -1

Qing Guo
Qing Guo

Reputation: 9112

Add formnovalidate to the first button input. And formnovalidate attribute could skip the validation in client side,but could not skip validation in server side. So you could clear model state to skip validation in server side. Below is a mvc demo, you can refer to it.

Custom.cs:

public class Custom
    {
        public string name{ get; set; }
        public int Id { get; set; }        
    }

In HomeController.cs:

 public IActionResult Submit()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Submit(Custom custom)
        {
            if (!ModelState.IsValid)
            {
                ModelState.Clear();//clear model state to skip validation in server side
                return View("Submit");
            }
            return View("Submit");
        }

View:

@model nnnn.Models.Custom

@{
    ViewData["Title"] = "Submit";
}

<h1>Submit</h1>

<h4>Custom</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Submit">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="name" class="control-label"></label>
                <input asp-for="name" class="form-control" />
                <span asp-validation-for="name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Id" class="control-label"></label>
                <input asp-for="Id" class="form-control" />
                <span asp-validation-for="Id" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" formnovalidate class="btn btn-primary" />
                <input  type="submit" value="Save"  class="btn btn-secondary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Result:

enter image description here

Upvotes: 2

serhatyt
serhatyt

Reputation: 425

I am sure there are many ways to handle this...

You can direct to different controllers with different buttons and one of them would be like in this link and there are differents ways in it that you can use.. Check this out. I hope this helps.

Disable Validation Link

Upvotes: 0

Related Questions