【问题标题】:MVC3 WebGrid column definition problemMVC3 WebGrid列定义问题
【发布时间】:2011-11-09 04:44:13
【问题描述】:

我的一个视图中有一个 WebGrid,如果用户是领导者,我想在其中一个列中显示图像。到目前为止,我所拥有的目前还行不通。有人知道我该怎么做吗?

我的麻烦代码:

grid.Column("FullName", header:"Name", format: (item) => (item.IsLeader())) ?
    @<text><img src="@Url.Content("~/Content/Images/Leader.gif")" alt="" /></text> :
    Html.ActionLink((string)item.FullName,"Index","Organization", new { area = "Catalogue" }, null)
),

我收到过载错误。 希望每个人都能看到我想在这里做的事情。 (我是 razor 和 WebGrid 的新手)

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    试试这个

    Func<dynamic, object> format = @<text>@{ 
        if (item.IsLeader())
        {
            <img src="@Url.Content("~/Content/Images/Leader.gif")" alt="" />
        }
        else
        {
            <text>Html.ActionLink((string)@item.FullName,"Index","Organization", new { area = "Catalogue" }, null).ToString()</text>;
        }}</text>;
    
    grid.Column(
        "FullName", 
        header:"Name", 
        format: format            
    )
    

    在此处查看有关 Razor 代表的更多信息:http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx

    【讨论】:

    • 没用,我认为你不能在 webgrid 定义中编写这样的 razor 语法
    • 抱歉,是的 :) 稍作修改应该可以,不是吗?只需将其定义为某个变量即可。
    • 你有例子吗?到目前为止,我已经尝试了大约 50 种方法:P
    • 那不行,因为“item”变量属于Webgrid,我不能在webgrid内部定义新变量,也不能在webgrid定义之外使用item(因为它不存在)。
    猜你喜欢
    • 2011-07-20
    • 1970-01-01
    • 2012-12-06
    • 2011-10-17
    • 2013-05-09
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 2011-06-19
    相关资源
    最近更新 更多