如何在ASP.Net MVC中创建动态HTML表格(webforms中的模拟HtmlTable) [英] How to create dynamic HTML table in ASP.Net MVC (analog HtmlTable in webforms)
本文介绍了如何在ASP.Net MVC中创建动态HTML表格(webforms中的模拟HtmlTable)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建具有复杂结构和不同样式单元格的动态表?
我有很多代码,例如:
How to create dynamic table with the complex structure and different styles of cells?
I have a lot of code such as:
在控制器中:
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代替吗?
Can I use HtmlHelper instead of this?
推荐答案
您应该将数据传递给控制器操作中的视图:
You should pass your data to the view in your controller action :
在控制器中:
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>
这篇关于如何在ASP.Net MVC中创建动态HTML表格(webforms中的模拟HtmlTable)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文