【问题标题】:How to create dynamic HTML table in ASP.Net MVC (analog HtmlTable in webforms)如何在 ASP.Net MVC 中创建动态 HTML 表(webforms 中的模拟 HtmlTable)
【发布时间】:2013-05-23 12:15:09
【问题描述】:

如何创建结构复杂、单元格样式不同的动态表格?
我有很多代码,例如:

在控制器中:

string styleTag = "class=\"someCssStyle\"";
ViewBag.htmlRawName1 += "<td colspan=\"" + colspan +
                    "\" " + styleTag + ">" + name + "</td>";
                for (int i = 0; i < list.count; i++)
                {
                    ViewBag.htmlRawName2 += "<td " + styleTag + ">" + list[i].Name +
                        "</td>";
                }

在视图中:

<table>
   <tr>
       @Html.Raw(ViewBag.htmlRawName1 )
   </tr>
   <tr>
       @Html.Raw(ViewBag.htmlRawName2 )
   </tr>
</table>

我可以使用 HtmlHelper 代替这个吗?

【问题讨论】:

    标签: asp.net-mvc html-helper html-table


    【解决方案1】:

    您应该在控制器操作中将数据传递给视图:

    在您的控制器中:

    List<YourClass> viewModel = list;
    ViewBag.NbColumns = 5; //Number of your table's columns
    return View(list);
    

    在你看来:

    @model List<YourClass>
    
    <table>
      <tr>
        <td colspan="@ViewBag.NbColumns">Name</td>
      </tr>
      <tr>
        @foreach(var item in Model) {
            <td class="someCssStyle">@item.Name</td>
        }
      </tr>
    <table>
    

    【讨论】:

      猜你喜欢
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      相关资源
      最近更新 更多