I’d like to think this is obvious, but before I submit a bug report, I want to know that I’m not doing it wrong. I have this view using ASP.NET MVC3 RC, with 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>
It chokes at “Location” and at the pipe in the last conditional. If I insert some <text> tags, it works like this:
<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>
Despite some trial and error, I can’t figure out what I’m doing that is naughty. Suggestions?
Your markup should be as follows
When you have an
@ifstatement, anything after the curlys is still considered to be ‘code’ so you need to break out of it by either using a<text>tag or the@:syntax.The reason for this behavior is that frequently you will have some sort of tag nested inside the conditional anyway, in which case things just work:
The
<text>tag is there for those case when you don’t want the contents of the conditional to be wrapped in any tags.