开发者

MVC3 - RenderPartial inside RenderSection not working

I'm working on an MVC3/Razor page, and in my _layout I have

  @RenderSection("relatedBooksContainer", false)

In another page I use that section with:

@section relatedBooksContainer
{
@{ Html.RenderPartial("~/Views/Shared/Bookshelf.cshtml", Model.Books);} 
}

This doesn't work. From what I've read, RenderSection will only ever go one layer deep - it has no concept of the Html.RenderPartial in the related section and will just return a blank area. The workaround I read at http://forums.asp.net/t/1590688.aspx/2/10 is to use RenderPage and commit the returned HTML to a string, then outout that string in the render section...which works! That is, until I pass a model to the partial page, then it throws an error saying:

The model item passed into the dictionary is of type 'TheBookshelf.ViewModels.BookshelfViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[TheBookshelf.EntityModel.Book]'.

Anyone have any idea why this might be happening? Are there any other ways to achi开发者_JAVA技巧eve this?


Try the @Html.Partial instead

@section relatedBooksContainer
{
  @{ Html.Partial("~/Views/Shared/Bookshelf.cshtml", Model.Books);} 
}


The error message is regarding the type and return type of the Bookshelf from the model.

    public IEnumerable<Book> Bookshelf()
    {
        var q = from book in bookshelf
                select book;
        IEnumerable<Book> myBooks = q.ToList<Book>();

        return myBooks;
    } 


did the solution in the provided link work for you?

I couldn't get it to work. That is I couldn't get ViewData["MainView"] to pass the data from Layout.cshtml to partialview. This aparently is a feature as every view is supposed to have it own ViewData obj. It seems ViewData is not global like I have thought. So what I get in ViewData["MainView"] from Layout in my partial view is null......I eventually found a work around for this and was able to pass the page reference from Layout to Partialview via a @Html.Action call from Layout -> Controller -> PartialView. I was able to get my partialview to access and write to the correct rendersection. However I want to call the same partialview many times in my Layout.cshtml. A subsequent call to the same Partialview again in the Layout, does not work, as the reference to layout has changed since the first call and rendersection update. So the code looks like this:

Layout.cshtml:

@RenderSection("Top", false)
@Html.Action("Load", "Home", new { viewname = "_testPartialView", pageref = this }) 
@Html.Action("Load", "Home", new { viewname = "_testPartialView", pageref = this }) 

Partial View:

@Model Models.testModel
@Model.Content

@{

    var md = (System.Web.Mvc.WebViewPage)@Model.pageRef;

    @*This check fails in subsequent loads as we get null*@
    if(md.IsSectionDefined("Footer")) {
        md.RenderSection("Footer");
    }
    else {
        md.DefineSection("Footer", () => { md.WriteLiteral("<div>My Contents</div>"); });
    }

}

}

controller:

 public ActionResult Load(string viewname, System.Web.Mvc.WebViewPage pageRef)
    {            
        var model = new Models.testModel { Content = new HtmlString("time " + i++.ToString()), pageRef = pageRef };
        return PartialView(viewname, model);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜