开发者

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?


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>
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜