MVC Razor 3 RC语法:错误或用户错误?

Jef*_*utz 5 asp.net-mvc razor

我想这很明显,但在我提交错误报告之前,我想知道我做错了.我使用ASP.NET MVC3 RC和Razor有这个视图:

<div class="miniProfile">
    Joined: @FormatTime(Model.Joined)<br />
    @if (!String.IsNullOrWhiteSpace(Model.Location)) {
        Location: @Model.Location<br />
    }
    Posts: @Model.PostCount<br />
    @Html.ActionLink("Full Profile", "ViewProfile", new { id = Model.UserID }, new { target = "_blank" }) | 
    @Html.ActionLink("Send Private Message", "SendNew", "PrivateMessages", new { id = Model.UserID }) | 
    @Html.ActionLink("Send E-mail", "Send", "Email", new { id = Model.UserID })
    @if (!String.IsNullOrWhiteSpace(Model.Web)) {
        | <a href="@Model.Web" target="_blank">Visit user Web site: @Model.Web</a>
    }
</div>
Run Code Online (Sandbox Code Playgroud)

它在"位置"和最后一个条件的管道中窒息.如果我插入一些<text>标签,它的工作原理如下:

<div class="miniProfile">
    Joined: @FormatTime(Model.Joined)<br />
    @if (!String.IsNullOrWhiteSpace(Model.Location)) {
        <text>Location: </text>@Model.Location<br />
    }
    Posts: @Model.PostCount<br />
    @Html.ActionLink("Full Profile", "ViewProfile", new { id = Model.UserID }, new { target = "_blank" }) | 
    @Html.ActionLink("Send Private Message", "SendNew", "PrivateMessages", new { id = Model.UserID }) | 
    @Html.ActionLink("Send E-mail", "Send", "Email", new { id = Model.UserID })
    @if (!String.IsNullOrWhiteSpace(Model.Web)) {
        <text>| </text><a href="@Model.Web" target="_blank">Visit user Web site: @Model.Web</a>
    }
</div>
Run Code Online (Sandbox Code Playgroud)

尽管有一些试验和错误,但我无法弄清楚我在做什么,这是淘气的.建议?

mar*_*ind 7

您的标记应如下所示

<div class="miniProfile">
  Joined: @FormatTime(Model.Joined)<br />
  @if (!String.IsNullOrWhiteSpace(Model.Location)) {
    <text>Location: @Model.Location<br /></text>
  }
  Posts: @Model.PostCount<br />
  @Html.ActionLink("Full Profile", "ViewProfile", new { id = Model.UserID }, new { target = "_blank" }) |
  @Html.ActionLink("Send Private Message", "SendNew", "PrivateMessages", new { id = Model.UserID }) |
  @Html.ActionLink("Send E-mail", "Send", "Email", new { id = Model.UserID })
  @if (!String.IsNullOrWhiteSpace(Model.Web)) {
    <text>| <a href="@Model.Web" target="_blank">Visit user Web site: @Model.Web</a></text>
  }
</div>
Run Code Online (Sandbox Code Playgroud)

当你有一个@if语句时,卷曲后的任何东西仍然被认为是'代码',所以你需要通过使用<text>标签或@:语法来突破它.

这种行为的原因是,无论如何,你经常会在条件内嵌入某种标记,在这种情况下,事情就会起作用:

@if(condition) {
    <div>Some content</div>
}
Run Code Online (Sandbox Code Playgroud)

<text>当您不希望将条件的内容包装在任何标记中时,标记就是那种情况.