I have a model property who's value can be required or not required on the view based on the value of another property. I have implemented it in the view as follows.
<input @Html.Raw(Model.IsRequired ? "required data-val=true' data-val-required='" + Model.Name + " selection is required.'" : "") asp-for="Name" class="form-control" />
if (Model.IsRequired)
{
<span asp-validation-for="Name" class="text-danger"></span>
}
As indicated based on the Required field value, the validation is applied or not applied.
I also had to add this bit of code.
$("#btnSubmit").on("click", function () {
$("#form").submit();
});
The code works fine in validation of the code, however the message does not get displayed. What am I missing?
I also tried using this answer and changed my validation span to as below, but it did not work either.
<span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>

asp-for.