用户配置文件MVC4 - 仅显示登录的用户数据 [英] User Profile MVC4 - Display logged in user data only

查看:56
本文介绍了用户配置文件MVC4 - 仅显示登录的用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户登录时,我希望他们只看到与索引页面上的id匹配的数据库条目



我目前正在使用以下作为票价获取正确的数据但在运行项目时出错



代码:

When a user logs in i want them to see only db entry that match there id on the index page

im currently using the below which works as fare as getting the correct data but get a error when i run the project

Code:

public ActionResult Index()
{
    return View(db.UserProfiles.ToList().Where(u => u.UserId == (int)Membership.GetUser().ProviderUserKey));
}







错误:

nullreferenceexception未被处理用户代码

对象引用未设置为对象的实例。




Error:
nullreferenceexception was unhandled by the user code
Object reference not set to an instance of an object.

推荐答案

要在MVC4中完全实现授权,您必须执行那更多的工作。我发现(并使用过)一篇很好的文章来解决这个问题:

http://primaryobjects.com/CMS /Article147.aspx [ ^ ]



像魅力一样工作,一步一步帮助你。



祝你好运!
To fully implement authorization in MVC4 you have to do a little more work then that. I found (and used) a great article for this problem :
http://primaryobjects.com/CMS/Article147.aspx[^]

Works like a charm and helps you through step by step.

Good luck!

好的,所以断开该行并开始将每个片段分配给变量,这样您就可以看到每个片段的返回内容。 db.UserProfiles 返回 null ,或 MemberShip.GetUser()返回 null
OK, so break that line down and start assigning each piece to a variable so you can see what each piece is returning. Either db.UserProfiles is returning null, or MemberShip.GetUser() is returning null.


问题的原因是没有[授权]在索引调用上面的方法



感谢您的帮助
Reason for the is issue was not have [Authorize] above the index call methode

Thanks for all your help


这篇关于用户配置文件MVC4 - 仅显示登录的用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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