0

Question Context I have a model that is a list that contains lists (I have a lot of unique required fields) I am trying to make a comparison UI that will compare the fields of 3 models to each other and if any are different, display the fields in a table.

I can do this the hard way by writing the following code for each field:

@if (Model[0].SubModel[0].field1 != Model[1].SubModel[0].field1 || Model[0].SubModel[0].field1 != Model[2].SubModel[0].field1)
{
    <tr> 
    <td class="col-lg-2">@Html.LabelFor(m => Model[0].SubModel[0].field1)</td>
    <td class="col-lg-2">@Html.DisplayFor(m => Model[0].SubModel[0].field1)</td>
    <td class="col-lg-2">@Html.DisplayFor(m => Model[1].SubModel[0].field1)</td>
    <td class="col-lg-2">@Html.DisplayFor(m => Model[2].SubModel[0].field1)</td>
    </tr>
}

But this model has many fields, so it will become very inefficient to write it all out; both in terms of code size and in time to write the code.

Question Is there a way to iterate through all of the field names in the model and use that as a variable in the LabelFor and DisplayFor so that I only have to write this once?

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
wlyte
  • 1
  • 2
  • I don't believe so, at least not in this format. I don't see how I would be able to compare the results of the three models using that format. – wlyte Mar 27 '23 at 17:02

0 Answers0