Adding an extension method to HtmlHelper in asp.net mvc4 application

Lamloumi Afif

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>
        }
    }
}
Roy Dictus

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using DataAnnotation localizer in your extension method for HtmlHelper in ASP.net core mvc 3.1

Extension Method for Default HtmlHelper in RazorPage (ASP NET CORE)

ASP.NET Core : HtmlHelper extension (migration issue)

asp.net mvc 4 linq extension method syntax join

Asp.Net MVC Call HtmlHelper with a variable

MVC HtmlHelper extension method not found unless a cast is used

Dependency Injection in HtmlHelper extension method?

ASP.NET MVC - JSON.NET extension method for controller

How to set up IIS6 to host ASP .NET 4 MVC4 application

Extension method not found when publishing ASP.NET MVC app

ASP.NET MVC SelectList extension method not selecting list item

ASP.NET MVC Postbacks and HtmlHelper Controls is not reflecting Model Changes

Validate date and datetime with Bootstrap datetime picker, Jquery validation plugin in an Asp.Net MVC4 application

How to force Motorola 2180 mobile computer to be recognized as mobile device in ASP.NET MVC4 application

How to run a asp.net MVC4 web application in RedHat OS

Using the validation message within an Asp.net mvc4 application

MSBuild compiling ASP.NET MVC4 application - designed using Visual Studio 2015

How to catch undefined api method calls in ASP.NET MVC4 Web API

How to create common method for web api Url in asp.net MVC4

sending image file to controller action method in asp.net mvc4

ASP.NET MVC4 routes

JSON in ASP.NET MVC4

ASP.NET MVC4 ActionFilters

Routing in Asp.Net MVC4

Routing ASP.NET MVC4

Unit testing HtmlHelper extension method fails

How do I call a method that returns a Task<T> From an ASP.Net MVC4 Controller Method?

.NET Core application, extension method not found

AJAX call in ASP.NET MVC application not calling Action method

TOP Ranking

HotTag

Archive