简体   繁体   English

MVC-如何使用@RenderPage(或其他方式)渲染页面和控制器?

[英]MVC- How can I render a page and the controller with @RenderPage (or with something else)?

My situation : I have an Index.cshtml -page and a hidden _Session.cshtml -page. 我的情况 :我有一个Index.cshtml和一个隐藏的_Session.cshtml I would like render the _Session.cshtml -page in my Index.cshtml -page. 我想在我的Index.cshtml _Session.cshtml中渲染_Session.cshtml

Problem: If I use the @RenderPage -statement it will render only the page and I get some Null-Exception because the application don't fill my model (I fill the model in the controller). 问题:如果我使用@RenderPage -statement,它将仅呈现页面,并且会出现Null-Exception,因为应用程序未填充我的模型(我在控制器中填充了模型)。

Hope you can help me 希望你能帮我

Thank you 谢谢

you have 2 options to display a partial view: 您有2个选项可显示部分视图:

option1: 选项1:

@Html.Partial("_Session",model)

option2: 选项2:

@{Html.RenderPartial("_Session",model);}

You need also to take care where the partial is located, if is located in the same folder with the Index, is ok, otherwise I suggest you to put the partial view in Shared folder. 您还需要注意局部文件的位置,如果与索引位于同一文件夹中是可以的,否则我建议您将局部文件视图放在“共享”文件夹中。

Why don't you just use it as a layout page? 您为什么不仅将其用作布局页面?

@{
     Layout = "~/Views/Shared/_Session.cshtml";
 }

@RenderPartial()当然是您要在此处执行的操作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM