Reputation: 1250
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.
Upvotes: 0
Views: 3146
Reputation: 32578
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);
} %>
Upvotes: 3