MVC:如何根据值更改Html.Grid行的颜色?

我想知道如何更改Html.Grid中行的颜色,如果’Completed’布尔属性等于true。 这是一个示例网格:

@Html.Grid(Model.ExampleList).Columns(c => { c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateRequested)).Named("Date Requested"); c.For(a => a.Comment).Named("Comment"); c.For(a => a.Completed).Named("Completed"); }) 

任何帮助将不胜感激。

谢谢。

我猜你的意思是根据模型属性的给定值更改网格中给定行的背景颜色。 如果是这样,您可以使用RowAttributes方法:

 @Html.Grid(Model.ExampleList) .RowAttributes(row => new Hash(@class => row.Item.IsFoo ? "redclass" : "normalclass")) .Columns(c => { ... }) 

尝试以下示例,假设您有一个要突出显示的属性,例如:

 public bool IsImportant { get; set; } 

使用网格转到您的视图,然后将其添加到结尾:

 @Html.Grid(Model).Columns(columns => { //all your coloumn stuff }).WithPaging(10).SetRowCssClasses(item => item.IsImportant ? "important" : string.Empty) 

然后确保您有对所需样式表的引用并添加如下内容:

 tr.important { background-color:rgba(205, 92, 92, 0.5) !important; //indian red with opacity set to 50% } 

我发现没有!important表达式,我的一些行样式没有被我的自定义样式覆盖。

希望这与Darin的答案一样有用,因为我在使用.RowAttributes遇到了.RowAttributes