Reputation: 1579
This question appear when I worked with partial view (MVC3/Razor), but I am sure - it's clear Razor Syntax question, not related direct to partial view. So - I have partial view Menu.cshtml with full markup as:
@model IEnumerable<SportsStore.WebUI.Models.NavLink>
@foreach(var link in Model)
{
@Html.RouteLink(link.Text, link.RouteValues);
}
No problem - "parent" view call it @{Html.RenderAction("Menu", "Nav");} and all work as magic. But, if I will edit the Menu.cshtml as:
@model IEnumerable<SportsStore.WebUI.Models.NavLink>
@foreach(var link in Model)
{
Html.RouteLink(link.Text, link.RouteValues);
}
(see - NO '@' before Html.RouteLink!) all just broke: now @{Html.RenderAction("Menu", "Nav");} output is totally empty, no one HTML tag.
Want to know - what is the difference between two piece of code? I assume @ before foreach also automatically "drop into" and apply to Html.RouteLink as well? So - am I wrong?
Upvotes: 2
Views: 1290
Reputation: 888185
The Html.RouteLink
method simply returns an IHtmlString
containing a link; it doesn't do anything else.
When you write @Html.RouteLink(...)
, you're printing this link to the page.
When you write Html.RouteLink(...)
, you're calling the method without doing anything with the link.
Thus, nothing happens.
Upvotes: 3
Reputation: 13373
The @ before the statement causes it to be written to the output stream. When you remove it, all you are doing is invoking a method call on the Html object.
The @-operator's purpose is two-fold. It can either be used to output an expression to the response stream like you see it used in the first example. The other function is to start a code block. You can see this second behavior used in your code example with the @foreach(var link in Model)
Upvotes: 3