Asp.net mvc HtmlHelper用于自定义日期

Asp.net mvc HtmlHelper用于自定义日期,asp.net-mvc,razor,Asp.net Mvc,Razor,尝试使用自定义的HtmlHelper输出日期时间,以便返回“今天XX:YY”、“昨天XX:YY”或“日期时间”(在这个阶段,确切的格式并不重要,只需要返回字符串即可) 很明显,我错过了一些重要和基本的东西,我应该知道得更清楚 我的助手: public static MvcHtmlString RecentDate<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func&l

尝试使用自定义的HtmlHelper输出日期时间,以便返回“今天XX:YY”、“昨天XX:YY”或“日期时间”(在这个阶段,确切的格式并不重要,只需要返回字符串即可)

很明显,我错过了一些重要和基本的东西,我应该知道得更清楚

我的助手:

        public static MvcHtmlString RecentDate<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression)
    {
        var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

        if (metadata.Model != null && metadata.Model as DateTime? != null)
        {
            DateTime dt = (DateTime)metadata.Model;
            if (dt.Date == DateTime.Now.Date)
            {
                return MvcHtmlString.Create("Today " + dt.ToShortTimeString());
            }
            else if (dt.Date == DateTime.Now.AddDays(-1).Date)
            {
                return MvcHtmlString.Create("Yesterday " + dt.ToShortTimeString());
            }
            else
            {
                return MvcHtmlString.Create(dt.ToShortDateString() + " at " + dt.ToShortTimeString());
            }
        }

        return MvcHtmlString.Create("Never");
    }
public static MvcHtmlString RecentDate(此HtmlHelper HtmlHelper,表达式)
{
var metadata=modelmetada.FromLambdaExpression(表达式,htmlHelper.ViewData);
if(metadata.Model!=null&&metadata.Model as DateTime?!=null)
{
DateTime dt=(DateTime)metadata.Model;
如果(dt.Date==DateTime.Now.Date)
{
返回MvcHtmlString.Create(“今天”+dt.ToShortTimeString());
}
else if(dt.Date==DateTime.Now.AddDays(-1.Date)
{
返回MvcHtmlString.Create(“昨天”+dt.ToShortTimeString());
}
其他的
{
返回MvcHtmlString.Create(dt.ToShortDateString()+”位于“+dt.ToShortTimeString());
}
}
返回MvcHtmlString.Create(“从不”);
}
cshtml:

@model xxx.Models.ForumLatestPosts
@{
    ViewBag.Title = "Latest Posts";
}

<h2>@ViewBag.Title.</h2>

<div>
    <hr />
    <dl class="dl-horizontal">
        <dt>Latest Posts</dt>
        <dd>
            <table>
                @foreach (var item in Model.Threads)
                {
                    <tr>
                        <td>
                            @Html.ActionLink(item.Title, "Thread", new { id = item.Id })
                        </td>
                        <td>
                            @item.Title
                        </td>
                        <td>
                            @Html.RecentDate(item.LastPostDate);
                        </td>
                    </tr>
                }
            </table>
        </dd>
    </dl>
</div>
@model xxx.Models.ForumLatestPosts
@{
ViewBag.Title=“最新帖子”;
}
@ViewBag.Title。

最新帖子 @foreach(Model.Threads中的变量项) { @ActionLink(item.Title,“Thread”,新的{id=item.id}) @项目.标题 @Html.RecentDate(item.LastPostDate); }
错误:

Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0411: The type arguments for method 'xxx.HtmlHelpers.RecentDate<TModel,TValue>(System.Web.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TValue>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Source Error:


Line 22:                         </td>
Line 23:                         <td>
**Line 24:                             @Html.RecentDate(item.LastPostDate);**
Line 25:                         </td>
Line 26:                     </tr>
“/”应用程序中出现服务器错误。 编译错误 描述:编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码。 编译器错误消息:CS0411:无法从用法推断方法“xxx.HtmlHelpers.RecentDate(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression)”的类型参数。尝试显式指定类型参数。 源错误: 第22行: 第23行: **第24行:@Html.RecentDate(item.LastPostDate)** 第25行: 第26行:
方法Decellation需要一个函数作为参数,但您给它一个属性

您可以尝试使用,而不是foreach并使用:

@Html.RecentDate(m=>m[i].LadtPostDate)

顺便说一下:您还可以使用自定义显示模板

@model DateTime

... Logic for displaying your date ...
那么视图将是这样的:

@Html.EditorFor(m=>m.LastPostDate, "NameOfTheEditorTemplate")

我刚刚发现了答案(昨晚和今早几个小时,才刚刚找到!)。我认为你是对的,尽管对我来说有效的解决方案是@Html.RecentDate(m=>item.LastPostDate);谢谢看看这篇编辑过的文章。对于帮助者,您可以在使用时使用for来保持NamingConventions。