Skip to content

Instantly share code, notes, and snippets.

@Rookian
Last active December 11, 2015 04:58
Show Gist options
  • Save Rookian/4549000 to your computer and use it in GitHub Desktop.
Save Rookian/4549000 to your computer and use it in GitHub Desktop.
public class PostAsListRenderer<T> : HtmlTableGridRenderer<T> where T : class
{
private readonly string _collectionName;
private string _previousHtmlFieldPrefix;
Guid _currentIndex;
bool _firstCellRendered;
public PostAsListRenderer(string collectionName)
{
_collectionName = collectionName;
}
protected override void RenderStartCell(GridColumn<T> column, GridRowViewData<T> rowData)
{
string attrs = BuildHtmlAttributes(column.Attributes(rowData));
if (attrs.Length > 0)
attrs = " " + attrs;
var hidden = string.Empty;
if (!_firstCellRendered)
{
hidden = string.Format("<input type=\"hidden\" name=\"{0}.index\" value=\"{1}\" />", _collectionName, _currentIndex);
}
_firstCellRendered = true;
RenderText(string.Format("<td{0}>{1}", attrs, hidden));
}
protected override void RenderRowStart(GridRowViewData<T> rowData)
{
_firstCellRendered = false;
_currentIndex = Guid.NewGuid();
Context.ViewData.TemplateInfo.HtmlFieldPrefix = string.Format("{0}[{1}]", _collectionName, _currentIndex);
base.RenderRowStart(rowData);
}
protected override void RenderGridStart()
{
_previousHtmlFieldPrefix = Context.ViewData.TemplateInfo.HtmlFieldPrefix;
base.RenderGridStart();
}
protected override void RenderGridEnd(bool isEmpty)
{
Context.ViewData.TemplateInfo.HtmlFieldPrefix = _previousHtmlFieldPrefix;
base.RenderGridEnd(isEmpty);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment