ASP.Net MVC 在母版页中显示会话值
当我在母版页中显示会话值 (<%: Session["companyname"].ToString() %>
) 时,页面上会显示以下信息 { CompanyName =测试公司}
。我怎样才能得到这个值?
感谢您的帮助!
When I display a session value in a master page (<%: Session["companyname"].ToString() %>
) the following information is displayed on the page { CompanyName = TestCompany}
. How do I get just the value?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以显示该值在会话中存储位置的代码,我更有可能提供帮助。不过,我建议您可能需要重新考虑直接在视图中使用会话中的值。在我看来,最好拥有一个所有视图模型派生自的基本视图模型,该模型上具有 CompanyName 属性以及母版页所需的任何其他常见属性。然后,您的母版页可以强类型化为基本视图模型,并且您可以使用模型中的值。我在几个项目中使用了这种模式并取得了良好的成功。将其与基本控制器结合使用,其中为
OnActionExecuted()
中的视图结果填充公共属性,它可以非常有效地减少代码重复和魔术字符串的使用。在你看来。型号:
控制器:
母版页:
If you can show the code where the value is stored in the session, it's more likely that I could help. I would suggest, though, that you might want to reconsider using the value from the session directly in your view. It would be better, in my opinion, to have a base view model that all of your view models derive from that has the CompanyName property, and any other common properties required by your master page, on it. Your master page, then, could be strongly-typed to the base view model and you could use the values from the model. I've used this pattern with good success on a couple of projects. Couple it with a base controller where the common properties are populated for view results in
OnActionExecuted()
, it can be very effective in both reducing code duplication and the use of magic strings in your views.Model:
Controller:
Master Page: