来自renderpage的变量

use*_*123 0 asp.net asp.net-mvc razor

我有index.cshtml看起来像这样

@RenderPage("header.cshtml")
@x
Run Code Online (Sandbox Code Playgroud)

header.cshtml上我有这个

@{
var x="hello there"

}
Run Code Online (Sandbox Code Playgroud)

X从价值header.cshtml没有被张贴在index.cshtml.

我知道还有其他方法可以做到这一点,比如助手和功能,但我该如何使这项工作?

在经典的asp中,这个工作和id喜欢遵循模式.

teo*_*kot 7

index.cshtml中,您可以像这样使用:

@RenderPage("header.cshtml", x)
Run Code Online (Sandbox Code Playgroud)

然后在header.cshtml中,您可以获得如下值:

@Page[0]
Run Code Online (Sandbox Code Playgroud)

在你的View.

但最好这样做,因为Page对象Dynamic打开View,index.cshtml:

@RenderPage("header.cshtml", new { MyParam = x})
Run Code Online (Sandbox Code Playgroud)

并且,header.cshtml:

@Page.MyParam 
Run Code Online (Sandbox Code Playgroud)

但我认为更好的使用RenderPartial方法.这是关于它的好文章.