【问题标题】:WebGrid: cannot convert from 'lambda expression' to 'System.Func<dynamic,object>'WebGrid:无法从“lambda 表达式”转换为“System.Func<dynamic,object>”
【发布时间】:2012-06-15 20:12:17
【问题描述】:

我正在尝试处理在 ASP.NET MVC3 WinGrid 中呈现时可能为空的 DateTime 的情况。尝试设置 WebGridColumn 时出现错误。我有一个正在工作,一个没有。正在工作的想法较少,因为 html 是在辅助函数中生成的。我无法弄清楚为什么理想的那个不起作用。

这是一个有效的方法:

$gridSummary.Column("OngoingDate", 
    header: "Ongoing", 
    format: Html.DateTimeActionLink, 
    style: "ongoingDate")

public static object DateTimeActionLink(this HtmlHelper htmlHelper, dynamic item)
{
    DateTime? linkDateTime = item.OngoingDate;
    if (linkDateTime != null && linkDateTime.HasValue)
    {
        var x = linkDateTime.Value.ToString("MM/dd/yyyy");
        return LinkExtensions.ActionLink(htmlHelper, x, "Edit", "MdsAsmtSectionQuestions", new { mdsId = item.OngoingId }, null);
    }

    return MvcHtmlString.Empty;
}       

这是不工作的一个:

    $gridSummary.Column("AssessmentInfo", header: "Open Type | ARD",
                        format: (item) =>
                        {
                            return Html.DateTimeActionLink(
                                item.AssessmentDate,
                                "MM/dd/yyyy",
                                x => Html.ActionLink(item.AssessmentInfo + " | " + x, "Edit", "MdsAsmtSectionQuestions", new { mdsId = item.OngoingId }, null));
                        },
                        style: "assessmentInfo")

    public static object DateTimeActionLink(this HtmlHelper htmlHelper, dynamic item, string format, Func<string, MvcHtmlString> actionLink)
    {
        Nullable<DateTime> linkDateTime = item;

        if (linkDateTime != null && linkDateTime.HasValue)
            return actionLink(linkDateTime.Value.ToString(format));

        return MvcHtmlString.Empty;
    }

【问题讨论】:

  • 我刚刚尝试了以下操作,将参数转换为 DateTime?我得到同样的错误。我是否误解了您发布的链接中的修复? gridSummary.Column("AssessmentInfo", header: "Open Type | ARD", format: (item) => DateTimeActionLink( (DateTime?)item.AssessmentDate, "MM/dd/yyyy", x => Html.ActionLink(item .AssessmentInfo + " | " + x, "编辑", "MdsAsmtSectionQuestions", new { mdsId = item.OngoingId }, null))

标签: c# asp.net-mvc-3 razor lambda webgrid


【解决方案1】:

相对于:

...
format: (item) =>
                        {
                            return Html.DateTimeActionLink(
                                item.AssessmentDate,
                                "MM/dd/yyyy",
                                x => Html.ActionLink(item.AssessmentInfo + " | " + x, "Edit", "MdsAsmtSectionQuestions", new { mdsId = item.OngoingId }, null));
                        }
...

试试:

...
format: (item) =>
                            Html.DateTimeActionLink(
                                    //added cast
                                    (Nullable<DateTime>)(item.AssessmentDate),
                                    "MM/dd/yyyy",
                                    //added cast
                                    x => Html.ActionLink((string)(item.AssessmentInfo) + " | " + x, "Edit", "MdsAsmtSectionQuestions", new { mdsId = item.OngoingId }, null));
...

【讨论】:

  • 不起作用。它所做的只是在 . 中显示代码
  • 已编辑,而不是"Html.DateTimeActionLink(...",替换为"@Html.DateTimeActionLink(...",注意@的使用
  • 抱歉,在 Html 前面添加 @ 也不起作用。
  • 好的,由于某种原因,“格式”在动态类型方面存在问题,我已经编辑了答案以正确显示这一点
  • 注意动态类型对象(Nullable 和字符串)之前的转换
【解决方案2】:

当前版本的 Razor 不能使用 lambda 表达式。基本的很酷,但除此之外它们就会崩溃。我认为 Razor 2.0 支持它,但我必须检查:)

使用 Html 帮助程序没有任何问题。这就是他们的目的。考虑到您基本上是在调用相同的代码。如果您打算在另一个位置使用辅助方法,那么您将不会有代码重复。保持干燥。

另外,我不知道为什么你有一个 $ 我相当肯定你需要一个 @ 符号,因为它是一个 c# 方法而不是 jQuery。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 2013-06-18
    • 2020-04-10
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    相关资源
    最近更新 更多