I used unobtrusive validation to validate the form in client side, If I click on submit button the validation messages appear, If I start typing in input field the validation message disappeared, The problem happen with select tag, If I select an item from the list, the validation message still appears.
Here is the Business
class
public class Business
{
[Key]
public int Id { get; set; }
[Required]
public string? Name { get; set; }
[Required(ErrorMessage = "Please select business type")]
public int? BusinessTypeId { get; set; }
public BusinessType? BusinessType { get; set; }
}
BusinessType
class
public class BusinessType
{
public int Id { get; set; }
[Required]
public string? Name { get; set; }
public ICollection<Business>? Businesses { get; set; }
}
And the select
tag
<select class="form-select" asp-for="BusinessTypeId" asp-items="@(ViewData["BusinessTypeList"] as SelectList)" data-control="select2" data-hide-search="true" data-placeholder="Select a Business type">
<option value="">Select Business Type</option></select>
<span asp-validation-for="BusinessTypeId" class="text-danger"></span>
2
Answers
Try this
Your Business Class
Then your view
Try
Html.DropDownListFor
to get dropdownlist.Inside Controller Action Method
Inside View
Add the following
jquery scripts
jquery.validate.js and jquery.validate.unobtrusive.js