2012-10-03 23 views
2

我正在使用MVC Contrib网格来渲染,排序和过滤我的数据网格,但我现在有问题,现在它已经升级为MVC3和Razor。我的列集合中的自定义方法不适用于aspx页面,并且Action列方法现在已过时。如何获得MVC的贡献网格自定义列表使用aspx

我用网格操作方法来呈现MVC 2我的专栏是这样的:

... 
<% Html.Grid(Model).Columns(column => { 
      column.For(x => x.Id); 
      column.For(x => x.Name); 
      column.For(x => x.Email); 
      column.For(x => x.DateOfBirth); 
      column.For("View User").Named("Tools").Action(x => { %> 
       <td> 
        <%= Html.ActionLink("View", "View", new { id = p.Id })%> 
        <%= Html.ActionLink("Edit ", "Edit", new { id = p.Id })%> 
        //Snip as there are too many tools :-) 
        //..... 
        <%= Html.ActionLink("Delete", "Delete", new { id = p.Id })%> 
       </td> 
      <% }); 
... 

现在有了最新的版本有一个自定义的方法,它取代了过时的操作方法。我研究了如何完成in here,现在基本适用于我,但是我在aspx视图(url等)中放弃了所有助手,现在需要在我的模型中使用另一种方法呈现我的内容,如下所示:

... 
<% Html.Grid(Model).Columns(column => { 
      column.For(x => x.Id); 
      column.For(x => x.Name); 
      column.For(x => x.Email); 
      column.For(x => x.DateOfBirth); 
      //the new custom column 
      column.Custom(Model.ToolsRenderer); 
      <% }); 
... 

以下称为ToolsRenderer的网格模型方法用于呈现我的html字符串。

public UserManagementViewModel : BaseModel { 
    //..snip 
    // 
    public object ToolsRenderer(Client client) 
    { 
     List<string> links = new List<string>(); 

     var editLink = new TagBuilder("a"); 
     // here is my biggest problem, before Html.ActionLink used to make 
     // sure that I don't have any missing links or help me if i need to 
     // refactor an action/controller name :-(
     editLink.Attributes["href"] = GetEditUserLink(client, HttpContext.Current.Request.Url.AbsoluteUri); 
     editLink.SetInnerText("edit"); 
     links.Add(editLink.ToString()); 
     ... 
     ...lots of links to be generated here 
     ... 
     return MvcHtmlString.Create(string.join(" |", links)) 
     } 
    //..snip 
} 

这工作现在,但有没有办法让我的aspx页面像下面的剃须刀视图工作?

@Html.Grid(Model).Columns(column => 
{ 
    column.For(x => x.Id). 
    column.For(x => x.Name); 
    column.Custom(@<td><a href='@Html.Actionlink("edit","user",new {id})' alt="@item.email"/><a></td>) 

}) 

我想说的是这样的:

...  
column.Custom(%><td><a href='<%=Html.Actionlink("edit","user",new {id})%>' alt="<%=item.email%>"/><a></td><%) 
... 

回答

3

终于设法找到周围工作有一定的帮助和挖掘,而不是使用自定义列,使用column.for和推入Html.Partial。类似下面的代码。

<% Html.Grid(Model).Columns(column => { 
     column.For(x => x.Id); 
     column.For(x => x.Name); 
     column.For(x => x.Email); 
     column.For(x => x.DateOfBirth); 

     // could use RenderPartial() to 
     column.For(x =>Html.Partial("UserActionColumn", x)); 

     <% }); 
1

对于剃刀情况@helper可用于自定义字段。

例如:

column.For(r => Status(r.Status)).Encode(false).Named("Status"); 

@helper Status(string value) 
{ 
    if (value == RequestStatus.Pending) 
    { 
     <span class="label label-info">@value</span> 
    } 
    ... 
} 
0

补充其他的答案,如果你想通过自定义列进行排序,不要忘了添加SortColumnName。如果您不这样做,网格将使用Named参数作为列名称,但Named也用于为该列提供标题。我刚刚头痛,因为我的专栏名称与标题不匹配:

column.For(c => "custom text").Named("Column Name").SortColumnName("ColumnName");