2011-06-05 77 views
3

我想知道如何改變Html.Grid中一行的顏色,如果'Completed'布爾屬性等於true。這裏是一個示例網格:MVC:如何根據值更改Html.Grid行的顏色?

@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"); 
     }) 

任何幫助將不勝感激。

謝謝。

回答

5

我想你的意思是根據模型屬性的給定值改變網格中給定行的背景顏色。如果是這樣你可以使用RowAttributes方法:

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

麻煩要使用內嵌式語法(如@ Html.Grid),我必須保持.RowAttributes()在同一行@Html聲明,否則我會得到'類是保留字'的錯誤。 – 2012-08-20 13:26:47

1

試試下面的例子中,假設你有你想強調如屬性:

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