0

我遇到了一个问题,试图让 foreach 代码块在 mvc 控制器和视图模型中工作。

我想要实现的是循环遍历日期时间值,如果小于 60 秒,则显示秒数。如果超过 60 秒但少于 1 小时显示分钟。否则显示完整的日期时间。

我可以让上述工作,但它只显示第一条记录。我曾尝试将 foreach 循环放在不同的地方,但似乎无法让它工作。

希望有一双新鲜的眼睛来帮助解决这个问题。

public class MostRecentPostsViewModel
    {
        public List<MembersForumProperties> SelectMostRecentForumPosts { get; set; }
        public string DateAndTimeOfForumPosts                          { get; set; }
    }

    public class IsPostLessThanOneHour
    {
        public static string DisplayPostInMinutesOrSeconds(string displyMostRecentForumPosts)
        {
            string displayTime = string.Empty;

            //foreach (var postTime in mv.SelectMostRecentForumPosts)
            //{
            //    dte = postTime.ForumMemberDateTimePostedPost;
            //}

            DateTime dtn = DateTime.Now;
            DateTime timeOfPost = Convert.ToDateTime(displyMostRecentForumPosts);

            TimeSpan ts = dtn - timeOfPost;
            if (ts.TotalSeconds > 0 && ts.TotalSeconds < 60)
            {
                displayTime = "about " + ts.Seconds + " seconds ago";
            }
            else if (ts.TotalSeconds > 61 && ts.TotalSeconds < 3600)
            {
                displayTime = "about " + ts.Minutes + " minutes ago";
            }
            else
            {
                displayTime = displyMostRecentForumPosts;
            }
            return displayTime;
        }
    }

控制器

public PartialViewResult MostRecentMembersPosts()
        {
            var displyMostRecentForumPosts = _imf.DisplayMostRecentForumPosts().ToList();
            var loopThroughDateTimes       = displyMostRecentForumPosts.ToList();
            var test = "";
            foreach (MembersForumProperties t in loopThroughDateTimes)
            {
                test = t.ForumMemberDateTimePostedPost;
            }

            var membersMostRecentPost = new MostRecentPostsViewModel
            {
              SelectMostRecentForumPosts = displyMostRecentForumPosts,

              DateAndTimeOfForumPosts = IsPostLessThanOneHour.DisplayPostInMinutesOrSeconds(test)
            };

            return PartialView("pvMostRecentMembersPost",membersMostRecentPost);
        }
4

1 回答 1

2

为什么不直接发送日期并使用像TimeAgo这样的JS插件

public PartialViewResult MostRecentMembersPosts()
{
    return PartialView("pvMostRecentMembersPost", _imf.DisplayMostRecentForumPosts().ToList());
}

那么在你看来

@model IEnumerable<MemberForumProperties>

<!-- Head section would need to be defined in your master page first -->
@section Head {
    <script src="jquery.timeago.js" type="text/javascript"></script>
    <script type="text/javascript">
        $.ready(function() {
            $("abbr.timeago").timeago();
        });
    </script>
}

@foreach (var m in Model)
{
    <abbr class="timeago" title='@m.ForumMemberDateTimePostedPost.ToString("s")' />   
}

TimeAgo 将负责将您的DateTime值转换为模糊时间戳。


问题

如果您不想采用客户端方法,那么要解决您当前的服务器端问题,您需要发送一个相对时间列表,此时您似乎只发送了最后一个相对时间,即

var test = "";
foreach (MembersForumProperties t in loopThroughDateTimes)
{
    test = t.ForumMemberDateTimePostedPost;
}
// test now contains the date/time of the last item in the `loopThroughDateTimes` list

var membersMostRecentPost = new MostRecentPostsViewModel
{
    SelectMostRecentForumPosts = displyMostRecentForumPosts,

    DateAndTimeOfForumPosts = IsPostLessThanOneHour.DisplayPostInMinutesOrSeconds(test)
};
// DateAndTimeOfForumPosts only contains the relative string for the last date/time

您当前的设置只是显得有点凌乱和杂乱,而且不太可读。

解决方案

整理一下这是我要做的

public static class DateTimeExt
{
    public static string ToRelativeTime(this DateTime value)
    {
        // you could move the entire implementation of `DisplayPostInMinutesOrSeconds` to here
        return IsPostLessThanOneHour.DisplayPostInMinutesOrSeconds(value);
    }
}
...

public PartialViewResult MostRecentMembersPosts()
{
    return PartialView("pvMostRecentMembersPost", _imf.DisplayMostRecentForumPosts().ToList());
}

然后在你看来

@model IEnumerable<MemberForumProperties>

@foreach (var props in Model)
{
    <p>@props.ForumMemberDateTimePostedPost.ToRelativeTime()</p>   
}
于 2013-10-22T11:50:13.130 回答