如何使用局部视图将行添加到主视图中定义的 table
How to use a partial view to add rows to a table defined in the main view
我的 MVC 3 主视图中有以下 table 定义。
<table id="myDynamicTable" class="myClass" >
<thead>
<tr id="uploadrow_0">
<th style="white-space: nowrap;display: inline;width: 5px; text-align: left " >
Number
</th>
<th style="white-space: nowrap;display: inline;width: 120px; text-align: left ">
Unit
</th>
<th style="white-space: nowrap;display: inline;text-align: left ">
Type
</th>
<th style="white-space: nowrap;display: inline;width: 90px; text-align: left ">
Date
</th>
<th style="white-space: nowrap;display: inline;width: 351px; text-align: left ">
Action
</th>
<th style="white-space: nowrap;display: inline;width: 300px; text-align: left ">
Comment
</th>
</tr>
</thead>
<tbody>
@if (Model.ProductDetails.Count > 0)
{
foreach (var item in Model.ProductDetails)
{
@Html.Partial("ProductDetailsPartial", item);
}
}
else
{
ProductDetailsViewModel item = new ProductDetailsViewModel();
item.Types = ViewBag.TypeList;
item.Unit = ViewBag.UnitList;
item.Number = 1;
@Html.Partial("ProductDetailsPartial", item);
}
</tbody>
</table>
我正在向上面动态添加行 table 但我添加的每一行都是从局部视图生成的。
下面是我的局部视图定义
@model ProductDetailsViewModel
<tr>
@using (Html.BeginCollectionItem("item"))
{
<td class="autoGenNumber" style="width: 5px" >
@if (Model == null)
{
ProductDetailsViewModel item = new ProductDetailsViewModel();
item.Types = ViewBag.TypeList;
item.Unit = ViewBag.UnitList;
item.Number = 1;
@Html.LabelFor(x => Model.Number, Model.Number.ToString(), new { style = "width: 10px;", @class = "autoGenNumber" })
}
</td>
<td class="tdUnit">
@Html.TextBox("Unit", Model.Unit, new { id = "Unit", style = "width:120px; padding: 0px;", @class = "txtUnit" })
</td>
<td class="tdBorder">
@Html.DropDownListFor(model => model.TypeId, new SelectList(Model.Types, "Value", "Text", Model.TypeId), "[--Select--]", new { @id = "ddlType", style = "width:20px; padding: 0px;", @class = "ddlType" })
</td>
<td class="tdBorder">
@if (Model.Date.ToShortDateString().Contains("1/1/0001"))
{
<input type="text" name="Model.Date" value="@DateTime.Today.ToShortDateString()" class="txtDate" readonly = "readonly" style = "width: 90px; padding: 0px;" />
}
else
{
<input type="text" name="Model.Date" value="@Model.Date.ToShortDateString()" class="txtDate" readonly = "readonly" style = "width: 90px; padding: 0px;" />
}
</td>
<td class="tdBorder">
@Html.DropDownListFor(model => model.UnitId, new SelectList(Model.Unit, "Value", "Text", Model.UnitId), "[--Select--]", new { @id = "ddlUnit", style = "width:351px;", @class = "ddlUnit" })
</td>
<td class="tdBorder">
@Html.TextBoxFor(x => Model.comment, new { id = "Comment", @class = "Comment RestrictCharacters", style = "width:300px;" })
</td>
}
</tr>
table 布局在 IE 8 及更高版本中正确显示,但在 chrome 和 Firefox 中,行列未与 table 列正确对齐 headers .
如何在主视图的 </tbody> </tbody>
元素
中将部分视图呈现为有效的 table 行
下面是我的 table 在 IE 中显示的截图(正确布局)
下面是 chrome
中的错误布局
可能 display: inline;
在 headers 上造成了这种效果。
通常 table 个单元格有 display:table-cell;
。
您的 table headers 应该类似于:
<th style="white-space: nowrap;width: 120px; text-align: left ">
Unit
</th>
我的 MVC 3 主视图中有以下 table 定义。
<table id="myDynamicTable" class="myClass" >
<thead>
<tr id="uploadrow_0">
<th style="white-space: nowrap;display: inline;width: 5px; text-align: left " >
Number
</th>
<th style="white-space: nowrap;display: inline;width: 120px; text-align: left ">
Unit
</th>
<th style="white-space: nowrap;display: inline;text-align: left ">
Type
</th>
<th style="white-space: nowrap;display: inline;width: 90px; text-align: left ">
Date
</th>
<th style="white-space: nowrap;display: inline;width: 351px; text-align: left ">
Action
</th>
<th style="white-space: nowrap;display: inline;width: 300px; text-align: left ">
Comment
</th>
</tr>
</thead>
<tbody>
@if (Model.ProductDetails.Count > 0)
{
foreach (var item in Model.ProductDetails)
{
@Html.Partial("ProductDetailsPartial", item);
}
}
else
{
ProductDetailsViewModel item = new ProductDetailsViewModel();
item.Types = ViewBag.TypeList;
item.Unit = ViewBag.UnitList;
item.Number = 1;
@Html.Partial("ProductDetailsPartial", item);
}
</tbody>
</table>
我正在向上面动态添加行 table 但我添加的每一行都是从局部视图生成的。
下面是我的局部视图定义
@model ProductDetailsViewModel
<tr>
@using (Html.BeginCollectionItem("item"))
{
<td class="autoGenNumber" style="width: 5px" >
@if (Model == null)
{
ProductDetailsViewModel item = new ProductDetailsViewModel();
item.Types = ViewBag.TypeList;
item.Unit = ViewBag.UnitList;
item.Number = 1;
@Html.LabelFor(x => Model.Number, Model.Number.ToString(), new { style = "width: 10px;", @class = "autoGenNumber" })
}
</td>
<td class="tdUnit">
@Html.TextBox("Unit", Model.Unit, new { id = "Unit", style = "width:120px; padding: 0px;", @class = "txtUnit" })
</td>
<td class="tdBorder">
@Html.DropDownListFor(model => model.TypeId, new SelectList(Model.Types, "Value", "Text", Model.TypeId), "[--Select--]", new { @id = "ddlType", style = "width:20px; padding: 0px;", @class = "ddlType" })
</td>
<td class="tdBorder">
@if (Model.Date.ToShortDateString().Contains("1/1/0001"))
{
<input type="text" name="Model.Date" value="@DateTime.Today.ToShortDateString()" class="txtDate" readonly = "readonly" style = "width: 90px; padding: 0px;" />
}
else
{
<input type="text" name="Model.Date" value="@Model.Date.ToShortDateString()" class="txtDate" readonly = "readonly" style = "width: 90px; padding: 0px;" />
}
</td>
<td class="tdBorder">
@Html.DropDownListFor(model => model.UnitId, new SelectList(Model.Unit, "Value", "Text", Model.UnitId), "[--Select--]", new { @id = "ddlUnit", style = "width:351px;", @class = "ddlUnit" })
</td>
<td class="tdBorder">
@Html.TextBoxFor(x => Model.comment, new { id = "Comment", @class = "Comment RestrictCharacters", style = "width:300px;" })
</td>
}
</tr>
table 布局在 IE 8 及更高版本中正确显示,但在 chrome 和 Firefox 中,行列未与 table 列正确对齐 headers .
如何在主视图的 </tbody> </tbody>
元素
下面是我的 table 在 IE 中显示的截图(正确布局)
下面是 chrome
中的错误布局可能 display: inline;
在 headers 上造成了这种效果。
通常 table 个单元格有 display:table-cell;
。
您的 table headers 应该类似于:
<th style="white-space: nowrap;width: 120px; text-align: left ">
Unit
</th>