I have a list of objects for my ASP.NET MVC model List<NameAddressModel>
. In this abbreviated example, we're only looking at the first name. The model has the [Required]
attribute, which will normally apply to all of the items, but I only need two of three InputFirstName
s filled out. What is the way to make the third item not required? Or do I have to do something different than using the [Required]
attribute this way?
My model class:
[Required]
[Display(Name = "First Name")]
public string? InputFirstName { get; set; }
The view:
for (var i = 0; i < 3; i++)
{
<div>
<label asp-for="InputFirstName[i]"></label>
<input class="form-control" asp-for=InputFirstName[i] />
</div>
}
Thanks!