Pergunta

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

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top