将数据传递给母版页的ASP.NET MVC [英] Pass data to Master Page with ASP.NET MVC
问题描述
我有一个混合ASP.NET的WebForms / MVC项目。在我的母版页,我有一个菜单中的用户控制和一个页脚用户控制。无论如何。我需要一些数据(2串)传递给我的菜单中的用户控制在我的母版页(选择在我的菜单导航等当前选项卡),我的意见强类型我的数据模型。我怎样才能从我的控制器将数据推到我的菜单或者至少让我的母版页访问某些数据$ P $在我的控制器对定义的?
I have a hybrid ASP.NET WebForms/MVC project. In my Master Page, I have a "menu" User Control and a "footer" User Control. Anyways. I need to pass some data (2 strings) to my "menu" User Control on my Master Page (to select the current tab in my menu navigation, etc.) My views are strongly-typed to my data model. How can I push data from my controller to my menu or at least allow my master page to access some data pre-defined in my controller?
的注:我知道这违反了纯粹的ASP.NET MVC,但就像我说的,这是一个混合的项目。我介绍了ASP.NET MVC到我的项目的主要目的是为了有超过我的某些情况下,用户界面更多的控制只。的
推荐答案
把你的串入的ViewData集合,
Put your strings into the ViewData collection,
ViewData["MenuString1"] = "My First String";
ViewData["MenuString2"] = "My Second String";
和检索它们在母版页是这样的:
and retrieve them in the Master Page like this:
myMenu.Property1 = ViewData["MenuString1"].ToString();
myMenu.Property2 = ViewData["MenuString2"].ToString();
http://nerddinnerbook.s3.amazonaws.com/Part6.htm
这篇关于将数据传递给母版页的ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!