15

我有以下我想模仿的遗留代码,所有操作链接在一列内。但是,我似乎无法正确使用 Razor 语法。我应该如何在 Razor 中表达这一点?

ASPX 列模板是这样的:

.Columns(column =>
{
    column.Template(o =>
        {%>
            <%= Html.ActionLink("Edit", "Edit", new{ id = o.DeviceID}) %> | 
            <%= Html.ActionLink("Delete", "Delete", new { id = o.DeviceID })%>
        <%});

我只能使用 Razor 获得三个单独的列,而不会抱怨语法等,如下所示:

.Columns(columns =>
{
    columns.Template(o => @Html.ActionLink("Edit", "Edit", new { id = o.ProductId })).Width(50);
    columns.Template(o => @Html.ActionLink("Details", "Details", new { id = o.ProductId })).Width(50);
    columns.Template(o => @Html.ActionLink("Delete", "Delete", new { id = o.ProductId })).Width(50);

如何使用 Razor 语法定义一个包含所有三个操作链接的模板列?

编辑: 在尝试以下对 Mike 答案的小改编时,我收到错误“只有赋值、调用、递增、递减和新对象表达式可以用作语句”:

columns.Template(o => @<text>@Html.ActionLink("Edit", "Edit", new { id = o.CampaignId }) | 
                        @Html.ActionLink("Delete", "Delete", new { id = o.CampaignId })
                        </text>).Width(100);
4

5 回答 5

27

这是一个显示绑定列和模板列的快速示例:

@<text></text>使用语法的示例 #1

@(Html.Telerik().Grid(Model)
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(m => m.UserName);
        columns.Bound(m => m.Email);
        columns.Template(@<text> @Html.ActionLink("Edit", "Edit", new { id = item.UserId} ) | 
                                 @Html.ActionLink("Delete", "Delete", new { id = item.UserId)  
                         </text>).Width(100);
    })
 )

使用 Action 委托的示例 #2

@(Html.Telerik().Grid(Model)
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(m => m.UserName);
        columns.Bound(m => m.Email);
        columns.Template(m => @Html.ActionLink("Edit", "Edit", new { id = m.UserId} ) + " | " +
                              @Html.ActionLink("Delete", "Delete", new { id = m.UserId)  
                         ).Width(100);
    })
 )

希望有所帮助,如果还没有弄清楚的话。:)

更新- 在上面的代码示例中添加了隐式定义的“项目”参数。它展示了如何在 Telerik 控件模板中获取模型属性。
UPDATE#2 - korchev 在他的代码示例中显示了“@item.someProperty”语法。在我们的例子中不需要 @ 符号,因为我们在扩展方法中,但为了清楚起见,保留它并没有什么坏处。 更新#3 - 添加了示例 #2 代码示例

于 2010-12-18T03:13:49.380 回答
2

如果您使用 ajax 绑定,格式必须看起来更像这样:

c.Bound(i => i.Name).ClientTemplate(@Html.ActionLink("<#= Name #>", "[Action]", "[Controller]", new { Id = "<#= Id #>" }, new { Area = "[Area]" }).ToHtmlString())

有关更多信息,请参见此处:http ://www.telerik.com/forums/kendo-mvc-grid-actionlink-column

于 2014-07-17T19:55:37.900 回答
1
columns.Template(@Html.ActionLink("Edit", "Edit", new {id = @item.id }));

查看 ScottGu 关于 Razor 的博客文章,了解 @item 是什么。

于 2010-12-18T12:34:58.657 回答
0
columns.Command(commands => {
 commands.Custom("Update").Text(Resource.Edit)
 .ButtonType(GridButtonType.BareImage)                                                .SendState(true).SendDataKeys(true).HtmlAttributes(new { id = "popUp"})   
                                              Action("Gallery_Bar_EditOrAddTranslate", "Administration").Ajax(false);
commands.Custom("Update").Text(Resource.Edit)
 .ButtonType(GridButtonType.BareImage)                                                .SendState(true).SendDataKeys(true).HtmlAttributes(new { id = "popUp"})                                                 Action("Gallery_Bar_EditOrAddTranslate", "Administration").Ajax(false);                                  }).Width("5%").Title(Resource.Coomand);

这将生成类似于操作链接 id id = m.UserId 的内容,您可以像 DataKeys 一样显示:

 .Name("GridName")  .DataKeys(key =>
                        {
                            key.Add(c => c.UserId).RouteKey("userId");

                        })

在 post 方法中,您将拥有:

public ActionResult xxx(int userId)
{
}
于 2013-09-11T11:42:01.527 回答
0

我有一个打印按钮,我需要把它放在标题行中,我选择把它放在与更新按钮所在的同一列中,但在更新按钮所在的位置上方。我能够像这样在 Razor 中做得很好:

columns.Command(command => {command.Edit(); }).Width(100).HeaderTemplate(i => @Html.ActionLink("Print Grid", "OutputAgencies", "Admin", new { @class = "k-button" }) );

这是我的链接按钮上显示“打印网格”的地方,“OutputAgencies”是我的控制器中的一个方法,“AdminController”是我的控制器的名称。

于 2014-11-08T02:58:58.710 回答