我想为 html 助手添加一个扩展方法来自定义表格,我添加了这个方法:
public static class HtmlElements
{
public static string Table(this HtmlHelper ht, string classe)
{
var table = new HtmlTable();
table.Attributes.Add("class", classe);
return table.InnerHtml;
}
}
当我想这样使用它的时候
@using(@Html.Table("table_data")){
}
我有一个错误,表明我必须将字符串转换为 IDisposable 类型。
- 这个错误的原因是什么?
- 我该如何解决?
编辑
完整 View 的代码:
using(var table = @Html.Table("table_data")){
<tr>
<th>Description</th>
<th>Client</th>
<th>Statut du client</th>
<th>Etat de test</th>
<th></th>
</tr>
for (int i = Model[2] - 5; i < Model[2]; i++)
{
if(i < Model[1].Count)
{
<tr style="font-size: 12px; padding:0px; ">
<td>@Model[0][i].PDescription</td>
<td>@Model[0][i].Nom_client</td>
<td>@Model[0][i].Statut_client</td>
<td style="color:red">@Model[1][i]</td>
<td>
@Model[0][i].Statut_client
</td>
</tr>
}
}
}
请您参考如下方法:
您的方法返回一个字符串,当您在 C# 中使用 using 时,这意味着您正在实例化一个实现了 IDisposable 的对象,这string 没有。
你也没有对字符串做任何事情。如果你打算建立一个 HtmlTable 并用它做一些事情,你必须修改你的代码,例如像这样:
public static HtmlTable Table(this HtmlHelper ht, string classe)
{
var table = new HtmlTable();
table.Attributes.Add("class", classe);
return table;
}
然后您必须在您的代码中使用它,如下所示:
@using(var table = @Html.Table("table_data")){
}
在括号内,您现在可以访问变量 table。
