我正在使用 MVC Contrib Grid 来渲染、排序和过滤我的数据网格,但现在它已针对 MVC3 和 Razor 进行了升级,我遇到了问题。我的列集合中的自定义方法不适用于 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>
            <% });
 ...

现在,在最新版本中,有一个自定义方法可以替代过时的操作方法。我研究了如何做到 在这里, ,现在基本上对我有用,但是我在 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><%)
...
有帮助吗?

解决方案

终于设法找到了一些帮助和挖掘的工作,而不是使用自定义列,使用column.for和push。类似于下面的代码。

<% 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));

        <% });
.

其他提示

对于剃刀案例@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>
    }
    ...
}
.

补充其他答案,如果您想按该自定义列排序,请不要忘记添加 SortColumnName. 。如果您不这样做,网格将使用 Named 参数作为列名,但是 Named 还用于为列提供标题。我只是很头疼,因为我的专栏名称与标题不匹配:

column.For(c => "custom text").Named("Column Name").SortColumnName("ColumnName");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top