so I can somehow pass value to the model to determine the parameter as
true or false
You can choose to set an custom model validation to finish it, this is this the official link.
And the following is my demo, hope it can help you.
Validation class:
public class RequiredExAttribute : ValidationAttribute
{
public string IsFalse { get; set; }
public string GetErrorMessage() => "Not this name";
public RequiredExAttribute(string name)
{
IsFalse = name;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value.ToString() == IsFalse)
{
return new ValidationResult(GetErrorMessage());
}
else return ValidationResult.Success;
}
}
Controller:
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(JobApplication jobApplication)
{
return View();
}
Model:
public class JobApplication
{
[RequiredEx("Tsai")]
public string Name { get; set; }
}
View:
@model JobApplication
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<form class="m-1 p-1" method="post">
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" type="text" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Submit Application</button>
</form>
<style>
.input-validation-error {
border-color: red;
}
</style>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
Result:
