Razor Partial View not Rendering
If I use the following Controller method:
public ActionResult Menu()
{
// do stuff...
return PartialView("viewName", navLinks);
}
calling the partial view in _Layout.cshtml like this:
<div id="categories">
@{ Html.Action(开发者_JAVA技巧"Menu", "Nav"); }
</div>
With the following ASCX partial view:
<%@ Control Language="C#"
Inherits="ViewUserController<IEnumerable<MyDataType>>" %>
<% foreach(var link in Model) { %>
<%: Html.Route.Link(link.Text, link.RouteValues) %>
<% } %>
everything works fine. Yay.
BUT, if I use either of the following RAZOR partial views:
@model IEnumerable<MyDataType>
@foreach(var link in Model){
Html.RouteLink(link.Text, link.RouteValues);
}
or...
@model IEnumerable<MyDataType>
@{
Layout = null;
}
@foreach(var link in Model){
Html.RouteLink(link.Text, link.RouteValues);
}
I get nothing. there's no exception thrown, I just don't get anything rendered. I know the problem isn't with the controller method (it works just great with the ASCX partial view).
What's going on here?
Try changing this:
@foreach(var link in Model){
Html.RouteLink(link.Text, link.RouteValues);
}
to this:
@foreach(var link in Model){
@Html.RouteLink(link.Text, link.RouteValues);
}
It looks like without the @ the method is being called, but the return value is just being dscarded. Putting the @ causes it to be written in the response.
The RenderAction
method writes the action directly to the view and returns void
.
The Action
method returns the action's contents but doesn't write anything to the view.
Writing @something
will print the value of something
to the page.
You cannot write @Html.RenderAction
, since RenderAction
doesn't return anything.
Writing Html.Action(...)
(without @
) calls the method normally, but doesn't do anything with its return value.
OK, Changing the way the it was called from _Layout.cshtml worked...
<div id="categories">
@Html.Action("Menu", "Nav");
</div>
It is important to note, that @Html.RenderAction DOES NOT work for me. I'd really love some explanation here, because right now, learning Razor is frustrating me as there is little documentation, and problems like these which should take minutes to resolve, are eating up way too much of my time.
精彩评论