我遇到了一个问题,试图让一个foreach代码块在mvc控制器和viewmodel中工作。
我想要实现的是循环播放日期时间值,如果小于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);
}
答案 0 :(得分: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
值转换为模糊时间戳。
问题
如果您不想采用客户端方法,那么要修复当前的服务器端问题,您需要发送一个相对时间列表,此时您似乎只是发送< em>最后相对时间即
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
您当前的设置显得有点凌乱&amp;混乱而且不太可读。
解决方案
稍微整理一下这就是我要做的事情
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>
}