2

I'm developing a layout page in MVC application. The layout page has - Header - Content - Footer

Header has ogo, Welcome message, user info, several other links.

My question is, Should i design header as partial view or render it using Rendoraction. PartialView is working fine. But as this data does not depend on rest of the page, i want to call a controller action which returns a header view with its own model.

but HTML Rendor action is not working.

_Layout.cshtml

<body>
    <div class="main_page">
        <div id = "header">
            *@Html.Partial("HeaderPartial", @Model)*@
            @Html.Action("Header")
        </div>        
        <div id="body">
            @RenderBody()
        </div>
        <div id="footer">
        </div>
</body>

HomeController

public ActionResult Header()
{
  Var VModel = //Create Model;
  return View(VModel)  
} 

header.cshtml

@model VModel
<div>
</div>

Error executing child request for handler

System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper

3 Answers 3

5

Your header.cshtml look's rare to me, but if you want to make your header view as a partial view

First:

Remove the layout using

@{
    Layout = null;
 }

Second To render this view on your layout use

@Html.Action("Header","Home", new { foo= @someElement}) <-- this element send values to your action
Sign up to request clarification or add additional context in comments.

4 Comments

I removed the layout as you said on header.cshtml and used '@Html.Action("Header", "Home")'. I'm still getting same error - Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
Code is same as above. IN Layout.cshtml I'm calling action method @Html.Action("Header", "Home") ActionMethod in HomeController gets called public ActionResult Header() { Var VModel = //Create Model; return View(VModel) } but breaks after returning the view(VMModel) with below error Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'
change the call from Action to RenderAction and put a try and catch to the whole controller
adding to that error message, this is the new error message i'm currenlty getting error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement"}
3

Since the Html.Action call is in the layout page you have to specify the controller

@Html.Action("Header", "SomeController")

Comments

3

If you want to use RenderAction call it like this in _Layout.cshtml

@{ Html.RenderAction("Header", "Home"); }

1 Comment

I tried that too, but i'm still getting the same error Error - Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.

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.