Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This is an implementation of a disposable HtmlHelper extension
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.Mvc;
namespace Demo.Helpers
{
public static class TableExtensions
{
public static MultiColumnTableHelper BeginMultiColumnTable(this HtmlHelper htmlHelper, params string[] headers)
{
return new MultiColumnTableHelper(htmlHelper.ViewContext.Writer, headers);
}
public class MultiColumnTableHelper : IDisposable
{
private TextWriter _textWriter;
public MultiColumnTableHelper(TextWriter textWriter, IEnumerable<string> headers)
{
_textWriter = textWriter;
var htmlBuilder = new StringBuilder();
htmlBuilder.Append("<table><thead><tr>");
foreach (var header in headers)
{
htmlBuilder.AppendFormat("<th>{0}</th>", header);
}
htmlBuilder.Append(@"</tr></thead>");
_textWriter.Write(htmlBuilder.ToString());
}
public void Dispose()
{
_textWriter.Write("</table>");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment