Error in MVC View page

Posted by Self-Innovator under ASP.NET MVC on 12/19/2014 | Points: 10 | Views : 682 | Status : [Member] | Replies : 1
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. I know it's a simple thing but i had been getting this error
Controller:HomeController
public ActionResult Index()
{
ViewBag.Countries = new List<string>()
{
"India",
"Japan",
"UK",
"USA"
};
return View();
}


My View:Index.cshtml

<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:


Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Ali.Karimi on: 12/20/2014 [Member] Starter | Points: 25

Up
0
Down
it says that need '{' after @foreach (string strCountry in ViewBag.Countries)
this shoud work:
    @foreach (string strCountry in ViewBag.Countries)
{
<li>
@strCountry
</li>
}



Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response