Partial view of a razor without rendering

If I use the following controller method:

public ActionResult Menu()
{
    // do stuff...

    return PartialView("viewName", navLinks);
}

calls a partial view in _Layout.cshtml as follows:

<div id="categories">
    @{ Html.Action("Menu", "Nav"); }
</div>

With the following partial view of ASCX:

<%@ Control Language="C#"
    Inherits="ViewUserController<IEnumerable<MyDataType>>" %>

<% foreach(var link in Model) { %>
    <%: Html.Route.Link(link.Text, link.RouteValues) %>
<% } %>

everything is working fine. Yay


BUT if I use any of the following partial RAZOR 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 is no exception, I just don't get anything. I know that the problem is not with the controller method (it works fine with the partial ASCX representation).

What's going on here?

+3
source share
3 answers

The method RenderActionwrites the action directly to the view and returns void.
The method Actionreturns the contents of the action, but writes nothing in the view.

@something something .
@Html.RenderAction, RenderAction .

Html.Action(...) ( @) , .

+5

:

@foreach(var link in Model){
    Html.RouteLink(link.Text, link.RouteValues);
}

:

@foreach(var link in Model){
    @Html.RouteLink(link.Text, link.RouteValues);
}

, @ , . @ .

+6

, , _Layout.cshtml, ...

<div id="categories">
    @Html.Action("Menu", "Nav");
</div>

, @Html.RenderAction . - , Razor , , , , , , .

+1

All Articles