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
- 将Class的实例作为参数传递给Attribute构造函数
- ASP.NET MVC中htmlAttributes的匿名类和IDictionary 之间的区别?
- 有没有什么好的理由不在核心MVC中使用ViewComponent而不是Partial View?
- Html.HiddenFor在ASP.NET中错误地格式化DateTime
- 使用entity framework将文件保存在数据库中
- Asp MVC:如何从ApplicationUser获取角色
- HttpApplication不会退出
- 在Html.BeginForm MVC4控制器动作中传递多个参数
- System.MethodAccessException,CAS和Visual Studio调试器