10

I was wondering, is it possible to create your own helper definition, with a using? such as the following which creates a form:

using (Html.BeginForm(params)) 
{
}

I'd like to make my own helper like that. So a simple example I'd like to do

using(Tablehelper.Begintable(id)
{
    <th>content etc<th>
}

which will output in my view

<table>
  <th>content etc<th>
</table>

Is this possible? if so, how?

Thanks

2 Answers 2

20

Sure, it's possible:

public static class HtmlExtensions
{
    private class Table : IDisposable
    {
        private readonly TextWriter _writer;
        public Table(TextWriter writer)
        {
            _writer = writer;
        }

        public void Dispose()
        {
            _writer.Write("</table>");
        }
    }

    public static IDisposable BeginTable(this HtmlHelper html, string id)
    {
        var writer = html.ViewContext.Writer;
        writer.Write(string.Format("<table id=\"{0}\">", id));
        return new Table(writer);
    }
}

and then:

@using(Html.BeginTable("abc"))
{
    @:<th>content etc<th>
}

will yield:

<table id="abc">
    <th>content etc<th>
</table>

I'd also recommend you reading about Templated Razor Delegates.

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

6 Comments

Ok, thanks. Since you did this in a class, I assume it isnt possible with a regular @helper Method()? Also, where do I place this? I tried just putting it in my app_code folder with the rest of the helpers, but that did not seem to work.
@RonSijm, you could place this class in a HtmlExtensions.cs file inside an Extensions folder for example. Just make sure that you bring the namespace into which this class is defined into scope in your view so that you can have access to the extension method: @using AppName.Extensions.
thanks, that seems to work. Too bad it does not work just putting the using in _layout.cshtml though.
@RonSijm, you could add the namespace to the <namespaces> section of ~/Views/web.config file (not ~/web.config). This way the helper will be universally available within your application. Or simply change its namespace to System.Web.Mvc.Html which is where all standard helpers are defined.
One thing to note... you should override the ToString() returning empty in the Table class... if not you will see the a text like YourDomain.HtmlExtensions.Table a long with the table.
|
0

Yes it is; however, to use Tablehelper.* you would need to subclass the base-view and add a Tablehelper property. Probably easier, though, is to add an extension method to HtmlHelper:

public static SomeType BeginTable(this HtmlHelper html, string id) {
    ...
}

which will allow you to write:

using (Html.BeginTable(id))
{
    ...
}

but this will in turn require various other bits of plumbing (to start the element at BeginTable, and end it in Dispose() on the returned value).

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.