MVC3 - RenderSection 内的 RenderPartial 不起作用

发布于 2024-11-05 06:50:50 字数 807 浏览 3 评论 0原文

我正在开发 MVC3/Razor 页面,在我的 _layout 中,我

  @RenderSection("relatedBooksContainer", false)

在另一个页面中使用该部分:

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

这不起作用。根据我的阅读,RenderSection 只会深入一层 - 它在相关部分中没有 Html.RenderPartial 的概念,只会返回一个空白区域。我在 http://forums.asp.net/t/1590688 阅读的解决方法。 aspx/2/10 是使用 RenderPage 并将返回的 HTML 提交到一个字符串,然后在渲染部分输出该字符串......这有效!也就是说,在我将模型传递到部分页面之前,它会抛出一个错误:

模型项传入 字典的类型是 'TheBookshelf.ViewModels.BookshelfViewModel', 但这本词典需要一个模型 项目类型 'System.Collections.Generic.List`1[TheBookshelf.EntityModel.Book]'。

任何人都知道为什么会发生这种情况?还有其他方法可以实现这一目标吗?

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 achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

背叛残局 2024-11-12 06:50:50

尝试使用 @Html.Partial

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

Try the @Html.Partial instead

@section relatedBooksContainer
{
  @{ Html.Partial("~/Views/Shared/Bookshelf.cshtml", Model.Books);} 
}
做个少女永远怀春 2024-11-12 06:50:50

错误消息与模型中 Bookshelf 的类型和返回类型有关。

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

        return myBooks;
    } 

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;
    } 
画离情绘悲伤 2024-11-12 06:50:50

提供的链接中的解决方案对您有用吗?

我无法让它发挥作用。也就是说,我无法让 ViewData["MainView"] 将数据从 Layout.cshtml 传递到partialview。这显然是一个功能,因为每个视图都应该有自己的 ViewData 对象。 ViewData 似乎并不像我想象的那样是全局的。因此,我从部分视图中的布局中的 ViewData["MainView"] 中得到的内容为空......我最终找到了解决此问题的方法,并且能够通过 @Html 将页面引用从布局传递到部分视图。来自布局的操作调用 ->控制器->部分视图。我能够让我的部分视图访问并写入正确的渲染部分。但是我想在 Layout.cshtml 中多次调用相同的局部视图。随后在布局中再次调用相同的 Partialview 不起作用,因为自第一次调用和渲染部分更新以来对布局的引用已更改。所以代码如下所示:

Layout.cshtml:

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

部分视图:

@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>"); });
    }

}

}

控制器:

 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);
    }

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文