使用Razor语法为Telerik MVC网格定义模板列 [英] Define a Template column for Telerik MVC Grid in Razor syntax

查看:97
本文介绍了使用Razor语法为Telerik MVC网格定义模板列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想模仿以下旧代码,所有动作链接都放在一栏中.但是,我似乎无法正确使用Razor语法.我应该如何在Razor中表达这一点?

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?

ASPX列模板是这样的:

The ASPX column template is like this:

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

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

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

.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语法定义一个包含所有三个操作链接的模板列?

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

编辑:在尝试对下面的Mike回答进行以下较小改编时,出现错误仅赋值,调用,递增,递减和新对象表达式可以用作语句":

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":

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

推荐答案

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

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

使用@<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样本

Sample #2 using an Action delegate

@(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. :)

更新-在上面的代码示例中添加了隐式定义的"item"参数.它显示了如何在Telerik控件模板中获取Model属性.
UPDATE#2 -korchev在他的代码示例中显示了"@ item.someProperty"语法.在我们的情况下,由于我们位于扩展方法之内,因此不需要@符号,但为了清晰起见,也不必担心. UPDATE#3 -添加了示例#2代码示例

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

这篇关于使用Razor语法为Telerik MVC网格定义模板列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆