Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text literal problem in MVC3 Razor view

Why am I required to use the <text> tag to enclose the pipe literal '|' in this markup? Surely it is well outside the scope of the ActionLink method.

@foreach (var item in Model.DetailItem.PlannedResources)
{ 
    <tr>
        <td>
            @if (Model.ViewMode == ViewMode.Edit)
            {
                @Html.ActionLink("Edit", "Edit", new { id = item.PlannedResourceId }) <text>|</text>
                @Html.ActionLink("Delete", "Delete", new {id = item.PlannedResourceId})                                                                     
            }
            @Html.ActionLink("Details", "Details", new { id = item.PlannedResourceId })
        </td>
        <td>
            | @item.ResourceType.Name
        </td>
    </tr>
}

If I don't use it, I get the error CS1525: Invalid expression term '|', but the second '|' gets by unhindered.

like image 213
ProfK Avatar asked Dec 11 '10 12:12

ProfK


People also ask

What is meaning of @: In .NET MVC?

Using @: to explicitly indicate the start of content We are using the @: character sequence to explicitly indicate that this line within our code block should be treated as content.

What do you have to do to escape email addresses in razor?

In Razor, `@` symbol is used to transition from HTML to C#. To escape an '@' symbol in razor markup, use two '@' symbols.

Is razor pages better than MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.


1 Answers

It's because when you are inside a statement with { and } only HTML tags are considered as literals, everything else is server side script. So you need to either use standard HTML tags such as <div>, <span>, ... or if you want to use a literal use the special <text> tag which is not outputted to the response.

like image 71
Darin Dimitrov Avatar answered Sep 28 '22 08:09

Darin Dimitrov