Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just did a simple Page in MVC where i was getting an error of parser. My code was everything fine. i just need some one what would cause this error.

Controller:HomeController
SQL
public ActionResult Index()
        {
            ViewBag.Countries = new List<string>()
            {
                "India",
                "Japan",
                "UK",
                "USA"
            };
            return View();
        }

My View:Index.cshtml
CSS
<h2>Countries List</h2>
<ul>
    @foreach (string strCountry in ViewBag.Countries)
        <li>
            @strCountry
        </li>
</ul>



My Error appears as :
Parser Error Message: Expected a "{" but found a "<". Block statements must be enclosed in "{" and "}". You cannot use single-statement control-flow statements in CSHTML pages. For example, the following is not allowed:
Posted

HTML
  @foreach (string strCountry in ViewBag.Countries){
        <li>
            @strCountry
        </li>
}

Try with this dear.
This will work out. As you are using loop that should have a start and close brackets.
Similar to server side code.
I hope this works.
Post back your queries if any.
Thanks.:)
 
Share this answer
 
Comments
King Fisher 22-Dec-14 6:42am    
good catch :)
[no name] 22-Dec-14 7:03am    
Thanks King.:)
HTML
<h2>Countries List</h2>
<ul>
    @{
       foreach (string strCountry in ViewBag.Countries)     
        <li>
            @strCountry
        </li>
     }
</ul>


I think the above code will hep u
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900