【问题标题】:Adding an Edit column to a telerik grid using ASP.Net MVC 2 and Telerik MVC (2010 Q1)使用 ASP.Net MVC 2 和 Telerik MVC (2010 Q1) 将编辑列添加到 Telerik 网格
【发布时间】:2011-01-19 09:11:02
【问题描述】:

我已经成功地创建了一个 Telerik 网格来显示产品列表,但是我在添加列以允许用户编辑时遇到了一些困难(我什至没有尝试在网格内进行编辑 - 我只是想要一个编辑视图的链接)

当我添加自定义列时,我在调试时在错误屏幕中看到以下行(第 24 行为红色):

Line 22:                          columns.Add(o => o.ProductIsActive);
Line 23:                          columns.Template(o =>
Line 24:                          {
Line 25:                              
Line 26:                              %><%=Html.ActionLink("Edit", "Edit", new { id = o.ProductID })%><% }).Title("Edit");

我的编译器错误消息是编译器错误消息:CS1525: Invalid expression term ')'

这是我的查看代码:

<%= Html.Telerik().Grid<NationalPetVax.Models.Product>()
          .Ajax(ajax => ajax.Action("_Index", "Products"))
          .DataKeys(dataKeys => dataKeys.Add(c => c.ProductID))
          .DataBinding(dataBinding => dataBinding.Ajax().Update("Update", "Home"))

          .Name("Grid")
                 .Columns(columns =>
                 {
                     columns.Add(o => o.ProductName).Width(81);
                     columns.Add(o => o.ProductPrice).Width(200);
                     columns.Add(o => o.ProductType.ProductTypeName);
                     columns.Add(o => o.Specy.SpeciesName);
                     columns.Add(o => o.ProductIsActive);
                     columns.Template(o =>
                     {

                         %><%=Html.ActionLink("Edit", "Edit", new { id = o.ProductID })%><% }).Title("Edit");

                     })
          .Sortable()
          .Scrollable()
          .Pageable();
    %>

有人见过这个问题吗?我一遍又一遍地遵循这些教程,并且即将完全放弃 Telerik 网格,尽管我真的很喜欢它们并且想将它们包含在我的项目中。

【问题讨论】:

    标签: asp.net-mvc gridview telerik editing


    【解决方案1】:

    我不知道 Telerik。但看起来问题出在表达式内的结束/开始标签上。试试这个:

    columns.Template(o =>
                  { 
                      Response.Write(Html.ActionLink("Edit", "Edit", 
                      new { id = o.ProductID })); 
                  }).Title("Edit");
    

    【讨论】:

    • 最佳答案因为似乎正确的解决方案存在问题:columns.Bound(o => o.Id).Format(Html.ActionLink("Edit", "Edit",新的 { id = "{0}" }).ToHtmlString()); - 它将 html 呈现为字符串。
    • @Merritt - 要使用该解决方案,您需要关闭 html 编码,Format(Html.ActionLink("Edit", "Edit", new { id = "{0}" }).ToHtmlString ()).Encoded(false);
    【解决方案2】:

    以下代码将解决您的问题,并使代码略显整洁。

    columns.Bound(o => o.ProductId).Format(
                 Html.ActionLink("Edit", "Edit", new {Id = "{0}"}).ToString());
    

    另外 Bound 是 2010 年第一季度发布的新 Add

    【讨论】:

      【解决方案3】:

      如果您想在代码中保留“鳄鱼标签”,例如

      columns.Template(o =>
                           {
      
                               %><%=Html.ActionLink("Edit", "Edit", new { id = o.ProductID })%><% }).Title("Edit");
      
                           })
      

      您只需要更改调用方式即可。在顶部你正在做一个

      <%=
      

      改成

      <%
      

      然后打电话

      .Render()
      

      在网格声明的末尾。这将防止“无效的表达式术语”错误。你的整个新代码应该是这样的

      <% Html.Telerik().Grid<NationalPetVax.Models.Product>()
                .Ajax(ajax => ajax.Action("_Index", "Products"))
                .DataKeys(dataKeys => dataKeys.Add(c => c.ProductID))
                .DataBinding(dataBinding => dataBinding.Ajax().Update("Update", "Home"))
      
                .Name("Grid")
                       .Columns(columns =>
                       {
                           columns.Add(o => o.ProductName).Width(81);
                           columns.Add(o => o.ProductPrice).Width(200);
                           columns.Add(o => o.ProductType.ProductTypeName);
                           columns.Add(o => o.Specy.SpeciesName);
                           columns.Add(o => o.ProductIsActive);
                           columns.Template(o =>
                           {
      
                               %><%=Html.ActionLink("Edit", "Edit", new { id = o.ProductID })%><% }).Title("Edit");
      
                           })
                .Sortable()
                .Scrollable()
                .Pageable()
                .Render();
          %>
      

      【讨论】:

        【解决方案4】:

        我想为代码添加一些评论。试试这个,成功了

        columns.Add(c => c.CustomerID).Format( Html.ActionLink("Edit", "Home", new { id = "{0}"}}) ).Encoded(false).Title("编辑");

        【讨论】:

          【解决方案5】:

          这是一个很晚的回复,但可能对其他人有帮助。 Telerik 网格不能仅在 Ajax 模式下使用服务器模板列。如果您只想在网格中添加一个额外的列,该列未绑定任何内容(同时仍保持 Ajax 模式),请尝试这样的操作

          columns.Template(o=>{}).ClientTemplate(
              Html.ActionLink("<Link text here>", "<action name>", "<controller name>", 
                  new { id = "<#= ID #>" }, new { @class = "Edit" }).ToString()
          ).Title("Edit Column")
          

          这将正确呈现,并且您想要链接的任何数据也将得到处理。

          【讨论】:

            猜你喜欢
            • 2023-03-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-23
            • 1970-01-01
            相关资源
            最近更新 更多