的WebGrid:无法转换lambda表达式中键入'对象',因为它不是委托类型 [英] Webgrid: Cannot convert lambda expression to type 'object' because it is not a delegate type

查看:274
本文介绍了的WebGrid:无法转换lambda表达式中键入'对象',因为它不是委托类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在列的WebGrid与此代码添加一个HTML图片:

Im trying to add in the webgrid column an html image with this code:

@model List<LoUCore.Models.Artifact>
@{    
   var grid = new WebGrid(Model);
   List<WebGridColumn> column = new List<WebGridColumn>();
   column.Add(new WebGridColumn { ColumnName = "Filepath", Header = "Sprite",Format = (x => @<text><img src="@x.Filepath"></img></text>) });
 }
@grid.GetHtml(columns: grid.Columns(column.ToArray()))

但即时得到以下错误:

CS1660: Cannot convert lambda expression to type 'object' because it is not a delegate type

任何想法?

推荐答案

您不能使用lambda表达式中的嵌入式剃刀字符串,如@Alessandro D'安德拉意味着你必须使用 X => < IMG SRC ='+ someString +'>< / IMG>中。而不是

You can't use embedded razor strings within the lambda expression, as @Alessandro D'Andra suggests you have to use x => "<img src='" + someString + "'></img>"; instead.

您可能还把它包起来一切都在 MvcHtmlString 来防止剃刀逃逸字符串一旦使用格式化,但我不知道格式是如何工作的 - 你必须试试吧。

You might also have to wrap it all in an MvcHtmlString to prevent Razor from escaping the string once it is used by the formatter, but I don't know exactly how the formatter works - you have to try it.

我做了一个小测试文件,找出到底是什么剃刀编译器会与您的代码做的。这是剃刀文件:

I made a small test file to find out exactly what the Razor compiler would do with your code. This is the razor file:

@{
    string someString = "somestring";

    Func<object, object> a = x => "<text><img src='" + someString + "'></img></text>";
    Func<object, object> b = x => @<text><img src="@someString"></img></text>);  
}



在ASP.NET编译器创建此C#代码出来的(只有相关的部分包括):

The ASP.NET compiler creates this C# code out of it (only relevant parts included):

    string someString = "somestring";

    Func<object, object> a = x => "<text><img src='" + someString + "'></img></text>";
    Func<object, object> b = x => 

            #line default
            #line hidden
item => new System.Web.WebPages.HelperResult(__razor_template_writer => {

BeginContext(__razor_template_writer, "~/Views/Home/Index.cshtml", 210, 4, true);

WriteLiteralTo(__razor_template_writer, "<img");

EndContext(__razor_template_writer, "~/Views/Home/Index.cshtml", 210, 4, true);

WriteAttributeTo(__razor_template_writer, "src", Tuple.Create(" src=\"", 214), Tuple.Create("\"", 231)

            #line 7 "c:\temp\MvcApplication1\Views\Home\Index.cshtml"
, Tuple.Create(Tuple.Create("", 220), Tuple.Create<System.Object, System.Int32>(someString

            #line default
            #line hidden
, 220), false)
);

BeginContext(__razor_template_writer, "~/Views/Home/Index.cshtml", 232, 7, true);

WriteLiteralTo(__razor_template_writer, "></img>");

EndContext(__razor_template_writer, "~/Views/Home/Index.cshtml", 232, 7, true);

使用嵌入式文本语法 @<文本>在lambda表达式中创建显然是不正确的C#代码。这是不足够的智慧使lambda表达式的嵌入式标签的一部分,而不是它打破通过插入代码来发出嵌入文本马上lambda表达式。

Using the embedded text syntax @<text> within the lambda expression creates obviously incorrect C# code. It is not smart enough to make the embedded tags part of the lambda expression, instead it breaks the lambda expression by inserting code to emit the embedded text right away.

这篇关于的WebGrid:无法转换lambda表达式中键入'对象',因为它不是委托类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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