2010-05-25 77 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); 
    ... 
})