动态生成的HTML表 - 如何约束宽度

时间:2011-07-25 09:02:15

标签: html css html-table

我正在使用MVC3创建动态表。

然而,表格在布局中离开页面并且不在共享div中?

@{
    ViewBag.Title = "Users";
}

<h2>Users</h2>

<p>
    @Html.ActionLink("Create New User", "Create")
</p>


    <table>
        <tr>
            <th>
                UserID
            </th>
            <th>
                UserName
            </th>
            <th>
                UserForename
            </th>
            <th>
                PW
            </th>
            <th>
                UserTypeID
            </th>
            <th>
                PasswordMustBeChanged
            </th>
             <th></th>
        </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
              @Html.DisplayFor(modelItem => item.U1_UserId)
            </td>
            <td>
                 @Html.DisplayFor(modelItem => item.U1_UserName)
            </td>
             <td>
               @Html.DisplayFor(modelItem => item.U1_UserForename)
            </td>
             <td>
               @Html.DisplayFor(modelItem => item.U1_PW)
            </td>
            <td>
                 @Html.DisplayFor(modelItem => item.U1_UserTypeID)
            </td>
             <td>
               @Html.DisplayFor(modelItem => item.U1_PasswordMustBeChanged)
            </td>
             <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.U1_UserId }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.U1_UserId })
            </td>       
        </tr>
    }

这是表,它在共享layotu @renderbody中呈现。但桌子不在div内。?

2 个答案:

答案 0 :(得分:1)

只是在黑暗中拍摄,但似乎你的标题部分还有额外的<th></th>?这是故意的吗?

答案 1 :(得分:0)

您可以在overflow: scroll;中的主要部分应用_Layout样式:

<section id="main" style="overflow:scroll;">
    @RenderBody()
</section>

这样,如果桌子的宽度大于主要部分的宽度,则会出现滚动条。