Monthly Archives: September 2013

MVC3 Webgrid – Custom column content


Notice that Street1 is rendered with ‘br’ as a string, not as HTML tag.
To fix that, Street2 uses MvcHtmlString.

var grid = new WebGrid(Model.Restaurants, rowsPerPage: 15, ajaxUpdateContainerId: "divListing");
@grid.GetHtml(columns: grid.Columns(
	grid.Column("CompanyName", "Name", style:"name"),
	grid.Column("Street", header: "Street1", format: (item) => item.StreetNumber + " " + item.Street + ", <br>" + item.PostalCode, style: "street"),
	grid.Column("Street", header: "Street2", style: "street",
		format: (item) => 
			new MvcHtmlString(
				item.StreetNumber + 
				" " + 
				item.Street + 
				(!string.IsNullOrWhiteSpace(item.Suite) ? ", Suite " + item.Suite : string.Empty) + 
				", <br />" + 
				item.PostalCode)
	),
	grid.Column("Cousines", "Cousines", style:"cousine")
	));

Thanks to: Ray Kuhnell
and
ASP.NET MVC3 webgrid conditional statement on column (if, if else loop)