Writing HTML Helpers
page 330,
book Professional ASP.NET MVC 3
For example in: \MvcApplication1\MvcApplication1\Core\Extensions.cs write something like:
using System;
using System.IO;
using System.Web.Mvc;
namespace MvcApplication1.Core
{
public static class HtmlExtensions
{
private class Extensions : IDisposable
{
private readonly TextWriter _writer;
public Extensions(TextWriter writer)
{
_writer = writer;
}
public void Dispose()
{
_writer.Write("</table>");
}
} //private class Extensions : IDisposable
//note that this part is not under private class Extensions : IDisposable
public static IDisposable Begin(this HtmlHelper html)
{
var writer = html.ViewContext.Writer;
writer.Write("<table>");
return new Extensions(writer);
}
}
}
And in: \MvcApplication1\MvcApplication1\Views\Stanko\Index.cshtml Something like:
@using (Html.Begin())
{
}
after starting app in the HTML source I will see tag <table></table> from here