ASP.NET MVC 3 Partial View in layout page

后端 未结 4 685
情歌与酒
情歌与酒 2021-01-04 11:53

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 navigati

相关标签:
4条回答
  • 2021-01-04 12:00

    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>
    
    0 讨论(0)
  • 2021-01-04 12:02

    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
    
    0 讨论(0)
  • 2021-01-04 12:09

    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

    0 讨论(0)
  • 2021-01-04 12:24

    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");}
    
    0 讨论(0)
提交回复
热议问题