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