Nathan Loding
Nathan Loding

Reputation: 3235

Razor engine not happy with my inline code

This code is copied directly from Scott Gu's blog (http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx):

Hello @name, the year is @DateTime.Now.Year

When I try this, it works:

@foreach (var post in ViewBag.Posts)
{
    @post.Title<br />
}

But when I try this, the code highlighter gets the whole thing as one Razor declaration and the compiler fails:

@foreach (var post in ViewBag.Posts)
{
    @post.Title, by @post.Author<br />
}

I get Compiler Error Message: CS1525: Invalid expression term ','. Again, with just @post.Title<br /> it compiles just fine.

What am I missing here?

Upvotes: 5

Views: 1635

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062600

If the content isn't obviously either language or content, you need to help it decide:

@foreach (var post in ViewBag.Posts)
{
    @:@post.Title, by @post.Author<br />
}

or:

@foreach (var post in ViewBag.Posts)
{
    <text>@post.Title, by @post.Author<br /></text>
}

or

@foreach (var post in ViewBag.Posts)
{
    <text>
    @post.Title, by @post.Author<br />
    </text>
}

Upvotes: 12

Related Questions