MVC3: Dynamically set row background color in a MVC WebGrid

The idea is to inject invisible div or hidden field with color value in the row which is supposed to be featured with specific class name.

 grid.Column("CompanyName", header: "Name", style: "name",
			format: (item) => new MvcHtmlString
				"<input type='hidden' class='color' value='#e0cca9' />" +
				"<input type='hidden' class='color' value='' />" +

Then on document load, grid reload, any form submission call JavaScript

function SetFeaturedRow() {
	$('.webgrid .color').each(function (index, element) {
		$(element).parent().parent().css('background-color', $(element).val());

Leave a Reply

Your email address will not be published. Required fields are marked *