I am trying to learn MVC and using samples from the "Pro ASP .net MVC 2". Only I'm trying to write everything in MVC3.
first I had some problem with @Html.RenderAction thing, I changed it to @Html.Action - it did the trick. Now I have a problem. Could you tell me why ascx view works and similar Razor doesn't?
ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<SportsStore.WebUI.Models.NavLink>>" %>
<% foreach (var link in Model) { %>
<%: Html.RouteLink(link.Text, link.RouteValues, new Dictionary<string, object> {
{ "class", link.IsSelected ? "selected" : null }
}) %>
<% } %>
Razor:
@model IEnumerable<SportsStore.WebUI.Models.NavLink>
开发者_如何学C@foreach (var link in Model)
{
Html.RouteLink(link.Text, link.RouteValues, new Dictionary<string, object> { { "class", link.IsSelected ? "selected" : null } });
}
The Html.RouteLink
method returns an IHtmlString
.
It does not write anything to the page.
You need to write the returned IHtmlString
to the page by writing @Html.RouteLink(...)
.
This is Razor's equivalent to the <%: Html.RouteLink(...) %>
in the ASCX page.
Try this:
@model IEnumerable<SportsStore.WebUI.Models.NavLink>
@foreach (var link in Model)
{
@Html.RouteLink(link.Text, link.RouteValues, new { @class = link.IsSelected ? "selected" : string.empty });
}
精彩评论