2010-05-25 88 views
3

我有一個MVCContrib網格,顯示來自Account對象的選定屬性。我希望用戶選擇一行並將其帶到另一個頁面,以查看它們單擊的行所代表的對象的完整屬性。如何將一個.Selected動作添加到網格的行中?MVCContrib網格 - 選擇行

回答

3

我今天剛剛遇到類似的問題。

可以使用.RowAttributes像這樣:

Html.Grid(Model).Columns(column => 
{ 
    column.For(e => e.Id); 
    column.For(e => e.Message); 
}) 
.RowAttributes(x => new Dictionary<string, object> 
    {{"onClick", "window.location.href = 'google.com'"}}) 
.Render(); 

在因此,當您在點擊它會觸發JavaScript的「點擊」,打開了谷歌。您可以通過使用Lamda中的「x」來更改網址以傳入Id。

3

如果您使用網格在MVC3情況下,還可以通過在服務器端的擴展類實現這一點:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using MvcContrib; 
using MySolution.ViewModels; 

namespace MySolution.Models.Extensions 
{ 
public static class RowAttributeExtensions 
{ 
    public static Hash GetRowAttributes(this MySolution.ViewModels.Model) 
    { 

     string onclickFunctionBody = "{window.location.href = '/MyController/MyAction?id=" + Model.Id + "'; return false;}"; 
     Hash hash = new Hash(onclick => onclickFunctionBody) 
     return hash; 
    } 
} 
} 

,並在客戶端,這將採取以下形式:

@Html.Grid(Model).RowAttributes(row => row.Item.GetRowAttributes()).Columns(column => 
{ 
    column.For(c => c.Col1); 
    column.For(c => c.Col2); 
    ... 
})