Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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("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?

like image 295
Didaxis Avatar asked Sep 13 '26 22:09

Didaxis


2 Answers

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.

like image 120
Danny Tuppeny Avatar answered Sep 17 '26 13:09

Danny Tuppeny


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.

like image 31
SLaks Avatar answered Sep 17 '26 13:09

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!