Note that there are some explanatory texts on larger screens.

plurals
  1. POforeach loop within mvc model
    primarykey
    data
    text
    <p>I'm stuck on a problem trying to get a foreach block of code to work within an mvc controller and viewmodel.</p> <p>What i'm trying to achieve is loop through the datetime values and if less than 60 seconds, show seconds. if more than 60 seconds but less than 1 hour show minutes. else show full datetime.</p> <p>I can get the above to work, but it only displays the 1st record. I have tried putting foreach loops in various places but just cannot seem to get it to work.</p> <p>Would appreciated a fresh pair off eyes to help with this.</p> <pre><code>public class MostRecentPostsViewModel { public List&lt;MembersForumProperties&gt; 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 &gt; 0 &amp;&amp; ts.TotalSeconds &lt; 60) { displayTime = "about " + ts.Seconds + " seconds ago"; } else if (ts.TotalSeconds &gt; 61 &amp;&amp; ts.TotalSeconds &lt; 3600) { displayTime = "about " + ts.Minutes + " minutes ago"; } else { displayTime = displyMostRecentForumPosts; } return displayTime; } } </code></pre> <p>Controller</p> <pre><code>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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload