从_layout.cshtml数据库加载数据 [英] Load data from database in _layout.cshtml

查看:1273
本文介绍了从_layout.cshtml数据库加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用ASP.NET MVC 4 Web应用程序。

I am creating a web application using ASP.NET MVC 4.

我想在所有的页面显示用户的昵称,所以我必须将其加载到 _layout.cshtml

I want to display the user's nickname on all of pages, so I have to load it in _layout.cshtml.

请注意,我手动存储用户的昵称。 (在我自己的数据库和我自己的表。)

Please note that I'm storing user's nickname manually. (In my own database and my own tables.)

我可以通过添加用户的用户名Membership.GetUser()。用户名,但我想从数据库中加载他的绰号,并显示在 _layout.cshtml

I can add user's User name by Membership.GetUser().UserName, but I want to load his nickname from database and show it in _layout.cshtml.

该怎么做?

推荐答案

我在登录劝你,用户名存储在会话变量(完成通过访问Session对象,它实现了一个key = value对架构,允许你存储和随意检索数据)。这是对业绩的拖累将不得不作出一个数据库调用每一个页面加载,并在会话建立一个昵称字符串变量不会占用您的服务器上的空间。

I would advise you to upon logging in, store the username in a session variable (which is done by accessing the Session object, which implements a key=value pair architecture allowing you to store and retrieve your data at will). It's a drag on performance to have to make a database call for EVERY page that loads, and a single nickname string variable in session won't take up that much space on your server.

至于加载你可以使用code发表的瓦希德变量,但我会建议你,如果你的网站仍处于起步阶段采取额外的时间来学习和实施实体框架。它是更清洁,更方便和微软真正推动数据连接管理这个方向努力。

As far as loading the variable you could use the code posted by Vahid, but I would recommend you if your site is still in its infancy to take the extra time to learn and implement Entity Framework. It is cleaner, easier and Microsoft is really pushing data connection management in that direction.

这篇关于从_layout.cshtml数据库加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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