11

I'm working on setting up a shared content (navigation) for an asp.net MVC layout page.

Here is my partial view "_LayoutPartial.cshtml" with code to pull navigation data from a model.

@model MyApp.Models.ViewModel.LayoutViewModel
<p>

    @foreach (var item in Model.navHeader)
    {
        //Test dump of navigation data
        @Html.Encode(item.Name); 
        @Html.Encode(item.URL); 

    }
</p>

Here is how the code for my controller "LayoutController.cs" looks like.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyApp.Models.ViewModel;

namespace MyApp.Controllers
{
    public class LayoutController : Controller
    {

        //
        // GET: /Layout/

        LayoutViewModel layout = new LayoutViewModel();

        public ActionResult Index()
        {
            return View(layout);
        }

    }
}

Here is the code for the "_Layout.cshtml" page. I'm attempting to call the partial view here using Html.RenderAction(Action,Controller) method.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
    <p>
        @{Html.RenderAction("Index","Layout");}
    </p>

    @RenderBody()
</body>
</html>

When the layout page executes the @{Html.RenderAction("Index","Layout");} line, it throws out an error message "Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'."

What am I missing friends? How can I call a partial view in a layout page?

Thank you all in advance!

2
  • Where is your partial view located Commented Jul 12, 2012 at 19:53
  • ~/Views/Shared/_LayoutPartial.cshtml Commented Jul 12, 2012 at 20:02

4 Answers 4

18

Instead of:

public ActionResult Index()
{
    return View(layout);
}

do:

public ActionResult Index()
{
    return PartialView(layout);
}

If you don't do that when you return a normal view from your child action, this normal view attempts to include the Layout, which in turn attempts to render the child action, which in turn returns a view, which in turn includes the Layout, which in turn attempts to render the child action, ... and we end up with names like the one ported by this very same site.

Also in your partial you don't need to do double encoding. The @ Razor function already does HTML encode:

@model MyApp.Models.ViewModel.LayoutViewModel
<p>

    @foreach (var item in Model.navHeader)
    {
        @item.Name 
        @item.URL
    }
</p>
Sign up to request clarification or add additional context in comments.

2 Comments

Throws "Object reference not set to an instance of an object." at the @foreach line in the partial view file. It seems like the navHeader object is coming with no items.
You can also just write @Html.Action("Index, "Layout") instead of @{Html.RenderAction("Index","Layout");} although I don't know which is faster.
6

First verify that your child view is inside the Shared directory

@Html.Partial("_LayoutPartial")

OR

 @{Html.RenderAction("actionname", "controller name");}

And don't use @Html.Encode(), Razor is already doing for u. Just use

@item.Name 
@item.URL

5 Comments

The first Html.Partial throws "Object reference is not set to an instance of an object" Error. The second suggestion throws the error message I posted in my 1st post.
Where is your partial view located, I mean it must be inside shared directory?
Yes the partial view is located inside the shared directory.
you are also not using IEnumerable<>, try this;
@model IEnumerable<MyApp.Models.ViewModel.LayoutViewModel>
2

I have solved this error getting on Layout page

System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper

Important ! First create partial view inside shared folder

In Controller ,

public PartialViewResult Userdetails()
{
   ....
   return PartialView("PartialViewName", obj);  
}

In Layout Page,

@{Html.RenderAction("action","controller");}

Comments

0

I know this is an old question but I thought I would throw this in here. You can use either Html.Action or Html.RenderAction. They both technically do the same thing but depending on how much content you're returning back can have an impact on which one you should really use for best efficiency.

Both of the methods allow you to call into an action method from a view and output the results of the action in place within the view. The difference between the two is that Html.RenderAction will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas Html.Action returns a string with the result.

Source

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.