【问题标题】:How to get the MVC contrib grid custom colum working with aspx如何让 MVCcontrib 网格自定义列与 aspx 一起使用
【发布时间】:2012-10-03 14:48:17
【问题描述】:

我正在使用 MVC Contrib Grid 来呈现、排序和过滤我的数据网格,但现在我遇到了问题,因为它已经针对 MVC3 和 Razor 进行了升级。我的 columns 集合中的 Custom 方法不适用于 aspx pages ,并且 columns Action 方法现在已经过时了。

我在 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>
            <% });
 ...

现在最新版本有一个自定义方法来替换过时的 Action 方法。我查看了它是如何完成的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><%)
...

【问题讨论】:

    标签: asp.net-mvc-3 datagrid mvccontrib mvccontrib-grid


    【解决方案1】:

    终于设法通过一些帮助和挖掘找到了解决方法,而不是使用自定义列,而是使用 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));
    
            <% });
    

    【讨论】:

      【解决方案2】:

      对于 Razor 案例,@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>
          }
          ...
      }
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多