I'm using FluentValidation with FormHelper in order to validate my form using ajax. My form has 2 submit buttons:
- Default one that should run validation (Save method in controller has [FormValidator] annotation)
- Second one (calls another controlers' method) should have validation disabled (Save method in controller has no validation annotation)
At this moment both buttons do POST call to Save method so validation run in both cases. If I remove FormHelper validation tags, client calls correct methods but there is no validation off course.
What should I do in order to enable validation only for one button?
View:
<form id="myFrom" asp-controller="MyController" asp-action="Save" method="post" asp-formhelper="true">
<label asp-for="MyField" class="form-label">My field: </label>
<input asp-for="MyField" class="form-control"/>
<span asp-validation-for="MyField" class="text-danger"></span>
<input type="submit" value="POST with validation">
<input type="submit" asp-controller="MyController" asp-action="AnotherAction" value="Post without validation">
</form>
Controller:
public class MyController
{
[HttpPost, FormValidator]
public async Task<IActionResult> Save(MyViewModel vm)
{
//do sth
}
[HttpPost]
public async Task<IActionResult> AnotherAction(MyViewModel vm)
{
//do another actions
}
}