如何申报ASP.NET MVC页的全局变量全局变量、ASP、NET、MVC

2023-09-04 22:41:53 作者:蹦跶蹦跶小心闪到腰

我已经开始非常最近与asp.net mvc的工作,我已经遇到了一个问题。 我有一个aspx页面这使得一些ASCX页。我想要做的是宣布在ASPX页面中的全局变量,因此可见其所有的孩子的。我试过<%VAR I = 0; %> ,但它不是在子页面可见

I've began working with asp.net mvc very recently and I've ran into a problem. I've got an aspx page which renders a few ascx pages. What I'd like to do is declare a global var at the aspx page so it is visible to all its childs. I tried <% var i = 0; %> but it wasn't visible at the child pages.

我能怎么办?

推荐答案

从一个aspx页面变量不与局部视图共享。 视图是一个数据的只是一个再presentation。你要传递的数据为模型,每个视图要呈现,无论是普通视图或PartialView。

variables from a aspx page are not shared with the partial views. The view is just a representation of a piece of data. You have to pass the data as a Model to each view you want to render, whether it's a plain View or a PartialView.

<% Html.RenderPartial("ViewName", Model, ViewDataDictionnary) %>

如果你想给一个变量传递给局部视图,我会强烈建议您将此参数添加到局部视图的模式,而这额外地通过ViewDataDictionnary通过。

If you want to pass a variable to a partial view, I would strongly recommend you to add this parameter to the model of the partial view, rather that to pass it additionally via the ViewDataDictionnary.