如何使用局部视图将行添加到主视图中定义的表

时间:2016-04-04 13:48:16

标签: html css asp.net-mvc asp.net-mvc-partialview

我在MVC 3主视图中有以下表格定义。

 <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>

我在上面的表中动态添加行,但我添加的每一行都是从局部视图生成的。

以下是我的部分视图定义

    @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>

表格布局在IE 8&amp;以上但在chrome和Firefox中,Row列未与表列标题正确对齐。

如何在主视图的 </tbody> </tbody> 元素

中将部分视图呈现为有效的表格行?

下面是我的表格如何出现在IE中的屏幕截图(正确的布局)

enter image description here

以下是chrome中错误的布局

enter image description here

1 个答案:

答案 0 :(得分:4)

标题上可能display: inline;会导致此类效果。

通常表格单元格为display:table-cell;

您的表格标题应如下所示:

<th style="white-space: nowrap;width: 120px; text-align: left ">
   Unit
</th>