3

I had a partial view as shown below in which i have first foreach loop for posts and another foreach to add comments for that post. But the run time error is as

Parser Error Message :Unexpected "foreach" keyword after "@" character. Once inside code, you do not need to prefix constructs like "foreach" with "@".

but the braces are perfect.

       @model Network.Models.ViewPostModel
    @using (Html.BeginForm("AddPost", "Network"))
{

        @Html.TextBoxFor(model => model.addpost.Content, new { @class = "textboxstyle"})
        <br/>
         <button type="submit">Add Post </button>
           <br/><br/>
           @foreach(var post in Model.Post)
           {
            <br/>
             <b>Posted by :</b> @post.Username
            <span> @post.Content </span> 
                @foreach(var comment in Model.Comments)
                {

                        <br/>
                       @comment.Content
                    <b>Comented  by :</b> @comment.username
                      <br/><br/>
               }
          @Html.TextAreaFor(model => model.addcomment.Content)
          <button type="submit">Add Comment </button>

        }
}

I tried either one foreach loop is working but not both .My Model class for strongly typed partial view is as below

public class ViewPostModel
    {

            public List<Post> Post { get; set; }
            public List<Comment> Comments { get; set; }
            public List<UserFriend> Userfriends { get; set; }
            public Post addpost { get; set; }
            public Comment addcomment { get; set; }

    }

Please check what my fault is in?

Thanks, michaeld

tereško
  • 58,060
  • 25
  • 98
  • 150
michaeld
  • 569
  • 1
  • 6
  • 10

1 Answers1

1

You seem to have the fieldset inside foreach and you're closing it outside.

I've seen similar problems with razor. e.g. check this post
The foreach block is missing a closing "}" character

And it doesn't make sense to me - you should move the first/opening outside the loop I think.

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • i think problem is not with fieldset , i edited removing it and also removed div tags , but still not working , one foreach is working fine , now the error message changed and also modified it. – michaeld Apr 22 '13 at 00:27
  • http://stackoverflow.com/questions/4946334/unexpected-foreach-keyword-after-character this worked for me thanks – michaeld Apr 22 '13 at 00:31
  • btw - it's not how SO works - if you find one error - you comment that, make an edit, pointing to another error - **but don't just remove it completely** turning it into a new error/question - as my answer then looks a bit foolish (but solved your original error spot on) – NSGaga-mostly-inactive Apr 22 '13 at 00:43
  • ohh i am sorry , i am very new to stackoverflow – michaeld Apr 22 '13 at 00:46