Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Razor 3 RC syntax: Bug or user error?

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?

like image 865
Jeff Putz Avatar asked Nov 10 '10 19:11

Jeff Putz


2 Answers

Your markup should be as follows

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

When you have an @if statement, 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:

@if(condition) {
    <div>Some content</div>
}

The <text> tag is there for those case when you don't want the contents of the conditional to be wrapped in any tags.

like image 134
marcind Avatar answered Nov 15 '22 08:11

marcind


You can't have just plain text content inside of a code block, the Razor engine can't figure out if it's code or markup. That's what the <text> tags are there, to remove the ambiguity. Are you saying that the <text> tags make it work (that's the answer, there's nothing more to be done), or that it still doesn't work with the <text> tags (try wrapping the whole if block in the <text> tag)?

like image 29
bdukes Avatar answered Nov 15 '22 09:11

bdukes