locked
load lists into table columns in razor RRS feed

  • Question

  • User-1556678718 posted

    Hi

    I have 5 lists with different number of elements

    then in the view I have table with 5 columns. In each column I should load different list. Is there a way to load the data by columns in the table not by rows?

    here is what i have:

     

    <div>
        <table class="table">
            <thead>
                <tr>
                    <th scope="col">Documents</th>
                    <th scope="col">Templates</th>
                    <th scope="col">Data</th>
                    <th scope="col">Macros</th>
                    <th scope="col">Css</th>
                </tr>
            </thead>
            <tbody>
                
           </tbody>
       </table>
    

    What do I add in <tbody>?

    Thanks

    Tuesday, June 4, 2019 5:47 AM

Answers

  • User-474980206 posted

    you just loop the the lists:

    @for(
        var i=0;  
        i < new int[]{
                  Model.List1.Count,
                  Model.List2.Count,
                  Model.List3.Count,
                  Model.List4.Count,
                  Model.List5.Count
                }.Max();
        ++i)
    {
        <tr>
           <td>@(Model.List1.Count > i ? Model.List1[i] : "")</td>
           <td>@(Model.List2.Count > i ? Model.List2[i] : "")</td>
           <td>@(Model.List3.Count > i ? Model.List3[i] : "")</td>
           <td>@(Model.List4.Count > i ? Model.List4[i] : "")</td>
           <td>@(Model.List5.Count > i ? Model.List5[i] : "")</td>
        </tr>
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2019 5:00 PM

All replies

  • User-1038772411 posted

    Hello RioDD,

    Please go through this below code for your reference, I hope this will help you

    <tbody>
            @foreach (var item in Model.Course)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.CourseID)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Title)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Credits)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Department.Name)
                    </td>
                    <td>
                        <a asp-page="./Edit" asp-route-id="@item.CourseID">Edit</a> |
                        <a asp-page="./Details" asp-route-id="@item.CourseID">Details</a> |
                        <a asp-page="./Delete" asp-route-id="@item.CourseID">Delete</a>
                    </td>
                </tr>
            }
        </tbody>

    For more details please refere the below link

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/read-related-data?view=aspnetcore-2.2&tabs=visual-studio

    Thank you

    Tuesday, June 4, 2019 6:20 AM
  • User-1556678718 posted

    hi,

    This is not the way.  I need to go by columns not by rows. Every column has different number of values. And they can't be each in separate partial view because those items are checkboxes.  Maybe i would succeed to put those lists in DataGrid and  display it like this but I'd rather go for a simpler solution.

    Tuesday, June 4, 2019 6:33 AM
  • User-474980206 posted

    you just loop the the lists:

    @for(
        var i=0;  
        i < new int[]{
                  Model.List1.Count,
                  Model.List2.Count,
                  Model.List3.Count,
                  Model.List4.Count,
                  Model.List5.Count
                }.Max();
        ++i)
    {
        <tr>
           <td>@(Model.List1.Count > i ? Model.List1[i] : "")</td>
           <td>@(Model.List2.Count > i ? Model.List2[i] : "")</td>
           <td>@(Model.List3.Count > i ? Model.List3[i] : "")</td>
           <td>@(Model.List4.Count > i ? Model.List4[i] : "")</td>
           <td>@(Model.List5.Count > i ? Model.List5[i] : "")</td>
        </tr>
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2019 5:00 PM