'How to access list of objects inside of model (parent object) using Razor pages
I would like to ask for help about asp.net core using Razor pages and how to access list inside of model to display them on page (cshtml). Hopefully picture will give a better explanation what I have and what I am trying to accomplish. Thanks for any help
This VnetObject class
public class VnetObject
{
public string VnetName { get; set; }
public string VnetRange { get; set; }
public string VnetDnsServers { get; set; }
public List<SubnetObject> SubnetObjects { get; set; }
}
This is SubnetObject class
public class SubnetObject
{
public string SubnetName { get; set; }
public string SubnetRange { get; set; }
public string SubnetNsg { get; set; }
}
This is code page Vnet.cshtml
@if (Model.VnetObjects != null)
{
@foreach (var vnetItem in Model.VnetObjects)
{
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.VnetObjects[0].VnetName)
</th>
<th>
@Html.DisplayNameFor(model => model.VnetObjects[0].VnetRange)
</th>
<th>
@Html.DisplayNameFor(model => model.VnetObjects[0].VnetDnsServers)
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Html.DisplayFor(modelItem => vnetItem.VnetName)
</td>
<td>
@Html.DisplayFor(modelItem => vnetItem.VnetRange)
</td>
<td>
@Html.DisplayFor(modelItem => vnetItem.VnetDnsServers)
</td>
<td>
<th></th>
</tr>
</tbody>
</table>
@foreach (var subnetItem in ) // What to write here to access SubnetObject
{
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor() // What to write here to access SubnetObject
</th>
<th>
@Html.DisplayNameFor()
</th>
<th>
@Html.DisplayNameFor()
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Html.DisplayFor() // What to write here to access SubnetObject
</td>
<td>
@Html.DisplayFor()
</td>
<td>
@Html.DisplayFor()
</td>
<td>
<th></th>
</tr>
</tbody>
</table>
}
}
}
How to access any property of VnetObject even if they are nested?
Solution 1:[1]
I just figured it out. For me it did not work using foreach loop but using for loop. Here is code I have used.
@if (Model.VnetObjects != null)
{
@for (var i = 0; i < Model.VnetObjects.Count; i++)
{
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.VnetObjects[0].VnetName)
</th>
<th>
@Html.DisplayNameFor(model => model.VnetObjects[0].VnetRange)
</th>
<th>
@Html.DisplayNameFor(model => model.VnetObjects[0].VnetDnsServers)
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Html.DisplayFor(model => model.VnetObjects[i].VnetName)
</td>
<td>
@Html.DisplayFor(model => model.VnetObjects[i].VnetRange)
</td>
<td>
@Html.DisplayFor(model => model.VnetObjects[i].VnetDnsServers)
</td>
<td>
<th></th>
</tr>
</tbody>
</table>
@for (var j = 0; j < Model.VnetObjects[i].SubnetObjects.Count; j++)
{
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.VnetObjects[0].SubnetObjects[0].SubnetName)
</th>
<th>
@Html.DisplayNameFor(model => model.VnetObjects[0].SubnetObjects[0].SubnetRange)
</th>
<th>
@Html.DisplayNameFor(model => model.VnetObjects[0].SubnetObjects[0].SubnetNsg)
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Html.DisplayFor(model => model.VnetObjects[i].SubnetObjects[j].SubnetName)
</td>
<td>
@Html.DisplayFor(model => model.VnetObjects[i].SubnetObjects[j].SubnetRange)
</td>
<td>
@Html.DisplayFor(model => model.VnetObjects[i].SubnetObjects[j].SubnetNsg)
</td>
<td>
<th></th>
</tr>
</tbody>
</table>
}
}
}
Formatting is not proper and displaying data in table needs to be fixed but how to display data is solved.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | filczek |