WebGrid: cannot convert from 'lambda expression' to 'System.Func<dynamic,object>'

大憨熊 提交于 2019-12-11 10:42:02

问题


I am trying to handle a case of a DateTime which might be null when rendering in a ASP.NET MVC3 WinGrid. I am getting an error when trying to set the WebGridColumn. I have one that I got working, one that isn't. The one that is working is less idea, for the html is generated within the helper function. What I cannot figure out is why the ideal one isn't working.

Here is the one that works:

$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;
}       

here is the one that is not working:

    $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;
    }

回答1:


Intead of:

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

Try:

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



回答2:


You can't use lambda expressions with the current version of Razor. Basic ones are cool but beyond that they break down. I think Razor 2.0 supports it but I'll have to check :)

There's nothing wrong with using Html helpers. That's what they're there for. Considering you're basically calling the same code. If you plan on using the helper method in another location then you won't have code duplication. Keep it DRY.

Also, I'm not sure why you have a $ I'm fairly certain you need an @ symbol since it's a c# method and not jQuery.



来源:https://stackoverflow.com/questions/11057583/webgrid-cannot-convert-from-lambda-expression-to-system-funcdynamic-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!