Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3320580
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:57:36+00:00 2026-05-17T22:57:36+00:00

Hi I’ve got problem with foreach statement. I have my controller method: public ActionResult

  • 0

Hi I’ve got problem with foreach statement. I have my controller method:

public ActionResult ShowComments(int id)
{
    EntryBox entrybox = new EntryBox();
    entrybox.entry = (from e in BlogDB.Enteries
                      where e.id == id
                      select e) as Entry;
    entrybox.comments = (from c in BlogDB.Comments
                         where c.EntryID == id
                         select c) as List<Comment>;
    Captcha captcha = new Captcha();

    ViewData.Model = entrybox;

    return View(); 
}

my viewModel:

public class EntryBox
{
    public Entry entry;
    public List<Comment> comments;
} 

and when I want to display list of my comments I’m not able to because of this error:

Error 2 foreach statement cannot operate on variables of type ‘void’ because ‘void’ does not contain a public definition for ‘GetEnumerator’
c:…\Views\Home\ShowComments.aspx

My 3 first line of View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<BlogMaker_v06.viewModels.EntryBox>" %>
<%@ Import Namespace="BlogMaker_v06.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentHolder" runat="server">
<% User LoggedAs = HttpContext.Current.Session["LoggedAs"] as User; %>

   <div style="width: 90%">
     <b>Treść komentowanego wpisu: <br /><br /></b>
       <p style="text-indent: 10px; text-align: justify;">
           <%= Model.entry.EntryContent.TagsParse()  %>
      </p>   
              <% Html.BeginForm("AddComment/" + ViewData["id"].ToString(), "Home");
        {     %>
        Write your comment: <br />
        <table>
        <tr><td>Name:</td><td><input type="text" name="Author" id="Author" value="<%= LoggedAs.Login %>" onfocus="javascript:this.value='';" <% if (LoggedAs.id != 2) {%> disabled <%}%>/></td></tr>
        <tr><td>Website:</td><td><input type="text" name="AuthorWebsite" id="AuthorWebsite" value="http://" /></td></tr>
        <tr><td>Przepisz cyframi: <%= ViewData["captcha"] %></td><td><input type="text" name="Captcha" id="Captcha" /></td></tr>
        </table>
        <textarea name="CommentContent" id="CommentContent" rows="5" cols="25" style="width: 100%; height: 10%; border: 1px solid #C0C0C0"></textarea><br />

        <input type="submit" value="Add comment" /> 
     <% }     %>

      <br /><br />
      <b>Komentarze użytkowników:</b> 
  <% foreach (var comment in ViewData.Model.comments.Reverse())
   {

       Html.RenderPartial("~/Views/Home/CommentTemplate.ascx", comment);    

 } %>
 </div>

I know it isnt good way to displaying data but I’m about refactory my whole project.
foreach displaying commentTemplate.ascx causes problem. Captcha is my simple way to authorize human being. and I’m sure it works well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:57:37+00:00Added an answer on May 17, 2026 at 10:57 pm

    Edit after seeing your foreach loop.

    Reverse() returns a void – it alters the list by reversing it, and does not return the list. You are trying to “foreach” over the return value of Reverse(), which is void – hence the error.

    You can either remove your call to Reverse() (and populate it in reverse order), or add something like this to the controller:

    entrybox.comments.Reverse();
    

    Or put it in your view if you must:

    <% ViewData.Model.comments.Reverse();
       foreach (var comment in ViewData.Model.comments)
       {
    
           Html.RenderPartial("~/Views/Home/CommentTemplate.ascx", comment);    
    
     } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.