MVC3 - RenderSection 内的 RenderPartial 不起作用
我正在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 @Html.Partial
Try the @Html.Partial instead
错误消息与模型中 Bookshelf 的类型和返回类型有关。
The error message is regarding the type and return type of the Bookshelf from the model.
提供的链接中的解决方案对您有用吗?
我无法让它发挥作用。也就是说,我无法让 ViewData["MainView"] 将数据从 Layout.cshtml 传递到partialview。这显然是一个功能,因为每个视图都应该有自己的 ViewData 对象。 ViewData 似乎并不像我想象的那样是全局的。因此,我从部分视图中的布局中的 ViewData["MainView"] 中得到的内容为空......我最终找到了解决此问题的方法,并且能够通过 @Html 将页面引用从布局传递到部分视图。来自布局的操作调用 ->控制器->部分视图。我能够让我的部分视图访问并写入正确的渲染部分。但是我想在 Layout.cshtml 中多次调用相同的局部视图。随后在布局中再次调用相同的 Partialview 不起作用,因为自第一次调用和渲染部分更新以来对布局的引用已更改。所以代码如下所示:
Layout.cshtml:
部分视图:
控制器:
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:
Partial View:
controller: