hsim
hsim

Reputation: 2090

Format a table in a view

In my app I have a view where I get a list of items based on this model:

public class ItemList
{
   public ObjInfo m_Obj { get; set;}

   public List<ObjData> m_ObjDataList { get; set;}
}

When I return the list, the m_Obj will always be linked. The list of ObjData will vary between 0 and 4, but I need to always show 4 slots for format purpose.

So here's how my view is structured:

<table>
    <tr>
        <th>
            Object Name
        </th>
        <th>
            Obj Data
        </th>
        <th>
            Date of Data
        </th>
    </tr>

    @for (int i = 0; i < Model.Count; i++)
    {      
        <tr>
            <th rowspan="4">
                @Html.ActionLink(Model[i].m_Obj.m_ObjName, "ShowObjData", new { id = Model[i].m_Obj.m_ObjID })
            </th>
            @for (int j = 0; j < Model[i].m_ObjDataList.Count; j++)
            {
                <tr>
                    <td>@Html.DisplayFor(item => Model[i].m_ObjDataList[j].m_DataProvider.m_DataProviderName)</td>
                </tr>
                <tr>
                    <td>@Html.DisplayFor(item => Model[i].m_ObjDataList[j].AnIntValue)</td>
                </tr>
                <tr>
                     <td>@Html.DisplayFor(item => item[i].m_ObjDataList[j].m_DateDataSaved)</td>
                </tr>
            }
        </tr>
    }
</table>

So, basically, I want to produce an effect like this:

Obj Name    Obj Data    Date of Data
Obj 1       Data 1          Date1
            Data 2          Date2
            Data 3          Date3
            Data 4          Date4
Obj 2       Data 1          Date1
            Data 2          Date2
            Data 3          Date3
            Data 4          Date4

But I'm having a fairly hard time formatting the values like I'd love to and that's why I'm asking for help.

Upvotes: 2

Views: 76

Answers (1)

hsim
hsim

Reputation: 2090

Found my answer after much work around!

I needed to format my table like this:

<table class="objItemTable">
    <tr>
        <th>
            Object Name
        </th>
        <th>
            Provider
        </th>
        <th>
            Obj Data
        </th>
        <th>
            Date of Data
        </th>
    </tr>

    @for (int i = 0; i < Model.Count; i++)
    {
        var className = i%2 == 0 ? "even" : "odd";

        <tr>
            <th rowspan="5">
                @Html.ActionLink(Model[i].m_Obj.m_ObjName, "ShowObjData", new { id = Model[i].m_Obj.m_ObjId })
            </th>
            @for (int j = 0; j < Model[i].m_ObjDataList.Count; j++)
            {
                <tr class="@className">
                    <td>@Html.DisplayFor(item => Model[i].m_ObjDataList[j].m_DataProvider.m_DataProviderNa,e)</td>
                    <td>@Html.DisplayFor(item => Model[i].m_ObjDataList[j].AnIntValue)</td>
                    <td>@Html.DisplayFor(item => item[i].m_ObjDataList[j].m_DateDataSaved)</td>
                </tr>
            }
        </tr>
    }
</table>

... Annnnd now it displays just fine. :)

Upvotes: 1

Related Questions