0

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 InputFirstNames 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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NovaDev
  • 2,737
  • 5
  • 29
  • 43
  • 3
    I think this is what you are after: [Conditionally required property using data annotations](https://stackoverflow.com/questions/26354853/conditionally-required-property-using-data-annotations) – JayV Aug 23 '23 at 15:33
  • @JayV - yes, that is. I couldn't figure out how to ask the right question. Thanks! – NovaDev Aug 23 '23 at 15:37

0 Answers0