10

I want to load partial view using

@Html.RenderAction("AccountNav", "Navigation", new { Area = "areassomething" })

but it throws an error.

Is there any alternative?

Is the semantic correct?

I have requirement such that I have to use something like this.

1
  • 1
    what error is throwing you? your code looks fine. Sep 14, 2016 at 6:31

3 Answers 3

18

If you simply want to render out a partial view (either view rendering or controller rendering), from another view or layout, you could use the Sitecore MVC extension methods:

Render view rendering: Html.Sitecore().ViewRendering("Path to the View")

Render controller rendering: Html.Sitecore().Controller("Controller Name", "Controller Action")

If you are using Sitecore 8.1 (or above) and want to include the Area part, I would recommend taking a look at the following blogpost, in order to see how this works:

https://ctor.io/new-in-sitecore-8-1-mvc-areas/

2
  • 1
    Thanks Soren, the render controller worked for me. With renderAction when i used like this @Html.RenderAction("AccountNav", "Navigation", new { Area = "something" }); It gives compile time error cannot implicitly convert void to object whereas when i just add curly braces like this it worked fine @{ Html.RenderAction("AccountNav", "Navigation", new { Area = "something" }); } Sep 14, 2016 at 12:33
  • There are performance implications with this method tho. It is generally better to use .Rendering() over .ViewRendering and .Controller - my answer has more detail
    – Richard Seal
    Sep 19, 2016 at 18:47
11

Although .ViewRendering and .Controller will work, there are performance issues with both of those methods.

Both methods will bypass Sitecore's output caching mechanism completely, so every page on your site that calls this will execute all the controllers. This may be the desired result, but most of the time we will want some kind of output caching on the rendering.

Using the code below, allows you to call a view or controller rendering and enable the Sitecore cache:

@Html.Sitecore().Rendering("{guid-to-rendering-item}", new { Cacheble=true, Cache_VaryByData=true })

Notice the explicit caching here and the vary by also.

The area's will be picked up in this case by the value set in the rendering item.

See this post for extra detail: https://cardinalcore.co.uk/2015/05/15/did-you-really-mean-controller-in-sitecore-mvc/

1
  • 1
    This is the best answer for Sitecore in my opinion. Generally if you have a controller or view rendering that your placing directly on a view (like on a layout for example) it's a great candidate for output caching and you should really go this route even if you don't plan to turn on Html Caching (you don't need to explicitly enable caching like Richard did in his example, you can also enable caching and set vary by logic in the content editor). Nov 3, 2016 at 1:56
3

Your compiler is right. Html.RenderAction() writes its output to the response stream and returns void. Html.Action() returns a string.

Use @{ Html.RenderAction(); } or @Html.Action() if you want to execute the actions directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.