MVC 模型中的每个循环

本文关键字:循环 模型 MVC | 更新日期: 2023-09-27 18:36:20

我遇到了一个问题,试图让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);
        }

MVC 模型中的每个循环

为什么不按原样发送日期并使用像 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>   
}