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

4

1 回答 1

1

你似乎有fieldset里面foreach,你把它关在外面。

我在剃须刀上看到过类似的问题。例如检查这篇文章
foreach 块缺少一个结束“}”字符

这对我来说没有意义 - 我认为你应该将第一个/开口移到循环之外。

于 2013-04-22T00:11:38.280 回答