1

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;
    }
Sam Carleton
  • 1,339
  • 7
  • 23
  • 45
  • possible duplicate of [ASP.NET MVC3 WebGrid format: parameter](http://stackoverflow.com/questions/5122691/asp-net-mvc3-webgrid-format-parameter) – Kirk Woll Jun 15 '12 at 20:14
  • I just tried the following, casting the param to a DateTime? and I get the same error. Am I missunderstanding the fix in the link you posted? gridSummary.Column("AssessmentInfo", header: "Open Type | ARD", format: (item) => DateTimeActionLink( (DateTime?)item.AssessmentDate, "MM/dd/yyyy", x => Html.ActionLink(item.AssessmentInfo + " | " + x, "Edit", "MdsAsmtSectionQuestions", new { mdsId = item.OngoingId }, null)) – Sam Carleton Jun 15 '12 at 20:36

2 Answers2

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));
...
JP _
  • 530
  • 4
  • 14
  • Does not work. All it does it display the code within the . – Sam Carleton Jun 15 '12 at 20:31
  • Edited, instead of "Html.DateTimeActionLink(...", replaced with "@Html.DateTimeActionLink(...", note the use of @ – JP _ Jun 15 '12 at 20:43
  • sorry, but adding the @ in front of the Html didn't work either. – Sam Carleton Jun 15 '12 at 21:04
  • OK, for some reason "format" is having trouble with dynamic types, I've edited the answer to show this correctly – JP _ Jun 15 '12 at 23:13
  • Note the cast before the dynamic typed objects (Nullable and string) – JP _ Jun 15 '12 at 23:20
  • JP Del Mundo, Thank you. There were two, one with a basic string, which I did not show. It worked once I tried Nullable. I tried DateTime? but that had not worked, didn't realize there would be a difference, but there was:) I also adding the cast the the string concatenation, which was required. I appreciate the help! – Sam Carleton Jun 18 '12 at 13:56
1

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.

Buildstarted
  • 26,529
  • 10
  • 84
  • 95