1

I would like to add an extension method for the html helper to customize a table, i add this method:

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

    }

When i'd like to use it like this

 @using(@Html.Table("table_data")){
}

i have an error which indicates that i have to convert a string to IDisposable type.

  1. What are the reasons of this error?
  2. How can i fix it?

Edit

the full view's code :

    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>
        }
    }
}
2
  • 1
    you add extension method for the htmlhelper, not the html table, also see more about using statement Commented Jan 14, 2014 at 8:57
  • why you want using? you can move creating html to your helper and return string with generated html, without object Commented Jan 14, 2014 at 9:37

2 Answers 2

3

Your method returns a string, and when you use using in C#, that means you are insantiating an object that implements IDisposable, which string does not.

You are not doing anything with the string either. If you intend to build up an HtmlTable and do something with that, you must modify your code, for instance like so:

    public static HtmlTable Table(this HtmlHelper ht, string classe)
    {
        var table = new HtmlTable();
        table.Attributes.Add("class", classe);
        return table;
    }

and then you must use that in your code, like so:

@using(var table = @Html.Table("table_data")){
}

and within the brackets, you can now access the variable table.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Roy , but it didn't work because : the generated html doesn't contain a table and the css class isn't added to the table
@Lamloumi: The code above doesn't generate HTML, you still have to add code between the { and } brackets to do something with the table variable.
@Lamloumi: You don't use the table variable anywhere, so of course it's not generating <table> tags. Also, the code in your question cannot possibly be the full View code -- Razor would not be able to parse it this way. Here's an article about Razor in MVC 4: jquery2dotnet.com/2012/08/mvc-4-razor-view-engine-syntax.html
1
public static class HtmlElements
{
    public static HtmlTable Table(this HtmlHelper ht, string classe)
    {
        var table = new HtmlTable();
        table.Attributes.Add("class", classe);
        return table;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.