如何申报ASP.NET MVC页的全局变量 [英] How to declare a global variable in ASP.NET MVC page

查看:266
本文介绍了如何申报ASP.NET MVC页的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始非常最近与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.

这篇关于如何申报ASP.NET MVC页的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆