的WebGrid:无法从'拉姆达前pression'转换';动态的,对象>&System.Func LT“ [英] WebGrid: cannot convert from 'lambda expression' to 'System.Func<dynamic,object>'

查看:120
本文介绍了的WebGrid:无法从'拉姆达前pression'转换';动态的,对象>&System.Func LT“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理一个ASP.NET MVC3 WinGrid呈现时可能为null一个DateTime的情况。尝试设置WebGridColumn时,我得到一个错误。我有一个,我有工作,一个不是。这是工作的一个是少的想法,因为HTML是辅助函数内产生。我想不出就是为什么理想的是行不通的。

下面是工作之一:

  $ gridSummary.Column(OngoingDate
    标题:正在进行,
    格式:Html.DateTimeActionLink,
    风格:ongoingDate)公共静态对象DateTimeActionLink(此的HtmlHelper的HtmlHelper,动态项)
{
    约会时间? linkDateTime = item.OngoingDate;
    如果(linkDateTime = NULL&放大器;!&安培; linkDateTime.HasValue)
    {
        VAR X = linkDateTime.Value.ToString(MM / DD / YYYY);
        返回LinkExtensions.ActionLink(的HtmlHelper,X,编辑,MdsAsmtSectionQuestions,新的{mdsId = item.OngoingId},NULL);
    }    返回MvcHtmlString.Empty;
}

在这里是不正常的:

  $ gridSummary.Column(AssessmentInfo,标题:开放型| ARD
                        格式:(项目)=>
                        {
                            返回Html.DateTimeActionLink(
                                item.AssessmentDate,
                                MM / DD / YYYY
                                X => Html.ActionLink(item.AssessmentInfo +|+ X编辑,MdsAsmtSectionQuestions,新的{mdsId = item.OngoingId},NULL));
                        },
                        风格:assessmentInfo)    公共静态对象DateTimeActionLink(此的HtmlHelper的HtmlHelper,动态的项目,字符串格式,Func键<字符串,MvcHtmlString>的ActionLink)
    {
        可空<&日期时间GT; linkDateTime =项目;        如果(linkDateTime = NULL&放大器;!&安培; linkDateTime.HasValue)
            返回的ActionLink(linkDateTime.Value.ToString(格式));        返回MvcHtmlString.Empty;
    }


解决方案

的这一翻译:

  ...
格式:(项目)=>
                        {
                            返回Html.DateTimeActionLink(
                                item.AssessmentDate,
                                MM / DD / YYYY
                                X => Html.ActionLink(item.AssessmentInfo +|+ X编辑,MdsAsmtSectionQuestions,新的{mdsId = item.OngoingId},NULL));
                        }
...

尝试:

  ...
格式:(项目)=>
                            Html.DateTimeActionLink(
                                    //添加投
                                    (可空<&日期时间GT;)(item.AssessmentDate)
                                    MM / DD / YYYY
                                    //添加投
                                    X => Html.ActionLink((字符串)(item.AssessmentInfo)+|+ X编辑,MdsAsmtSectionQuestions,新的{mdsId = item.OngoingId},NULL));
...

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

解决方案

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

这篇关于的WebGrid:无法从'拉姆达前pression'转换';动态的,对象&gt;&System.Func LT“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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