Skip to main content
 首页 » 编程设计

c#之如何在 .CSHTML 中自动调整列宽

2024年02月13日11cloudgamer

因此在 MVC 中,我使用 VS2013 的强大功能自动生成列表类型的 View ,该 View 使用 foreach 以数组格式显示对象列表及其属性。如何修改代码以自动调整列宽以适应?

<table class="table"> 
    <tr> 
        <th align="center"> 
            @Html.DisplayNameFor(model => model.IsoCode) 
        </th> 
        <th> 
            @Html.DisplayNameFor(model => model.Name) 
        </th> 
        <th align="center"> 
            @Html.DisplayNameFor(model => model.Continent) 
        </th> 
        <th align="center"> 
            @Html.DisplayNameFor(model => model.CountryId) 
        </th> 
        <th align="center"> 
            @Html.DisplayNameFor(model => model.TotalLanguages) 
        </th> 
        <th></th> 
    </tr> 
 
@foreach (var item in Model) { 
    <tr> 
        <td align ="center"> 
            @Html.DisplayFor(modelItem => item.IsoCode) 
        <td> 
            @Html.DisplayFor(modelItem => item.Name) 
        </td> 
        <td align ="center"> 
            @Html.DisplayFor(modelItem => item.Continent) 
        </td> 
        <td align ="center"> 
            @Html.DisplayFor(modelItem => item.CountryId) 
        </td> 
        <td align ="center"> 
            @Html.DisplayFor(modelItem => item.TotalLanguages) 
        </td> 
        <td> 
            @*@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |*@ 
            @Html.ActionLink("Details", "Details", new { id = item.CountryId }) 
            @*@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })*@ 
        </td> 
    </tr> 
} 

请您参考如下方法:

首先,确保有足够的空间来显示表格单元格中的所有内容。如果您为表格指定了明确的宽度(例如,使用像素作为测量单位),您可能会无意中限制了表格呈现其内容所允许的空间。这是您可以尝试的方法

  • 如果为表格或单元格设置了明确的宽度,则将其删除或使用动态单位(例如百分比)
  • 为表格单元格设置white-space nowrap

试一试,看看效果如何