Asp.net mvc 如何呈现来自帮助程序的HtmlTable

Asp.net mvc 如何呈现来自帮助程序的HtmlTable,asp.net-mvc,asp.net-mvc-3,c#-4.0,Asp.net Mvc,Asp.net Mvc 3,C# 4.0,我有一个助手,它返回一个htmlTable供查看。守则: public static HtmlTable Sales(this HtmlHelper helper) { HtmlTable tabela = new HtmlTable(); HtmlTableRow rowHeader = new HtmlTableRow(); HtmlTableCell cellCode = new HtmlTableCe

我有一个助手,它返回一个htmlTable供查看。守则:

public static HtmlTable Sales(this HtmlHelper helper)
        {
            HtmlTable tabela = new HtmlTable();
            HtmlTableRow rowHeader = new HtmlTableRow();
            HtmlTableCell cellCode = new HtmlTableCell();
            HtmlTableCell cellUnd = new HtmlTableCell();
            HtmlTableCell cellDescription = new HtmlTableCell();
            HtmlTableCell cellQtd = new HtmlTableCell();

            tabela.Width = "800px";

            cellCode.InnerText = "Código";
            cellUnd.InnerText = "Unidade";
            cellDescription.InnerText = "Descrição";
            cellQtd.InnerText = "QTD";


            cellCode.Focus();

            rowHeader.Cells.Add(cellCode);
            rowHeader.Cells.Add(cellUnd);
            rowHeader.Cells.Add(cellDescription);
            rowHeader.Cells.Add(cellQtd);

            tabela.Rows.Add(rowHeader);

            return tabela;
        }
但我的观点是:

System.Web.UI.HtmlControls.HtmlTable

我的返回是HtmlTable,如何在视图中呈现?

如评论中所述,此答案仅适用于使用Razor ViewEngine的情况

问题是Razor引擎不知道如何处理System.Web.UI.HtmlControl.HtmlTable

您应该尝试使用常规HTML标记构建表,例如:

public static MvcHtmlString Sales(this HtmlHelper helper)
        {


            MvcHtmlString tabela = new MvcHtmlString(@"
<table style=""width: 800px"">
    <thead>
        <tr>
            <th>Código</th>
            <th>Unidade</th>
            <th>Descrição</th>
            <th>QTD</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
");

            return tabela;
        }

然后,您可以传递数据并用值填充表格。

视图用于包含html输出。。使用不同的助手方法并不能帮助您使代码无法维护

这取决于您使用的视图类型。如果您使用的是Razor Viewengine,那么代码中将使用大量@,那么您应该返回一个MvcHtmlString对象,而不是表web部件。您在那里使用的UI元素似乎是一个aspx UI元素,我对它没有经验:如果是WebForms而不是Razor,我的答案就会从窗口出来。