asp.net-mvc 使用 Razor 语法为 Telerik MVC Grid 定义模板列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4463143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:38:38  来源:igfitidea点击:

Define a Template column for Telerik MVC Grid in Razor syntax

asp.net-mvctelerikasp.net-mvc-3razortelerik-mvc

提问by ProfK

I have the following legacy code that I would like to mimic, with all action links inside one column. However, I can't seem to get the Razor syntax right. How should I express this in Razor?

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

The ASPX column template is like this:

ASPX列模板是这样的:

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

I have only been able to get three separate columns using Razor without complaints about syntax etc. as below:

我只能使用 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);

How can I define one template column that contains all three action links using Razor syntax?

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

EDIT:In trying the following small adaptation of Mike's answer below, I get the error "Only assignment, call, increment, decrement, and new object expressions can be used as a statement":

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

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

回答by mikekidder

Here is a quick sample showing both bound columns and a template column:

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

Sample #1 using @<text></text>syntax

使用@<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);
    })
 )

Sample #2 using an Action delegate

使用 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);
    })
 )

Hope that helps, if didn't already figure it out. :)

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

UPDATE- added the implicitly defined "item" parameter in code sample above. It shows how to get the Model properties within the Telerik control template.
UPDATE#2- korchev showed the "@item.someProperty" syntax within his code sample. The @ symbol is not needed in our case since we are within an extension method, but doesn't hurt to leave it for clarity. UPDATE#3- added Sample #2 code sample

更新- 在上面的代码示例中添加了隐式定义的“item”参数。它展示了如何在 Telerik 控件模板中获取模型属性。
更新#2- korchev 在他的代码示例中展示了“@item.someProperty”语法。在我们的例子中不需要 @ 符号,因为我们在一个扩展方法中,但为了清楚起见,留下它也无妨。 更新#3- 添加了示例 #2 代码示例

回答by Josh

If you are binding with ajax, the format has to look something more like this:

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

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

See here for more info: http://www.telerik.com/forums/kendo-mvc-grid-actionlink-column

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

回答by Atanas Korchev

columns.Template(@Html.ActionLink("Edit", "Edit", new {id = @item.id }));

Check ScottGu's blog posts with regard to Razor for what @item is.

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

回答by Nic

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

This will generate something like action Link the id id = m.UserId you can show like DataKeys:

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

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

                        })

at the post method you will have :

在 post 方法中,您将拥有:

public ActionResult xxx(int userId)
{
}

回答by vapcguy

I had a print button I needed to put in the Header row, that I chose to put in the same column as, but above where the Update button goes. I was able to do it just fine in Razor like this:

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

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

This is where "Print Grid" was for display on my linkbutton, "OutputAgencies" was a method in my controller, and "AdminController" was the name of my controller.

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