如何显示当前登录的用户详细信息 [英] How to display current logged in users details

查看:97
本文介绍了如何显示当前登录的用户详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在aspnetusers表中添加了一些字段,如firstname lastname



这是我的控制器



i have added some fields in the aspnetusers table like firstname lastname

here is my controller

public ActionResult profile(string id)
        {
            var user = db.Users.Single(u => u.UserName == id);
            return View(user);

        }





和我的观点: -



and my view:-

@model MVCProfile.Models.ApplicationUser

@{
    ViewBag.Title = "profile";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Users Profile</h2>


Firstname:@Model.FirstName<br/>
Lastname:@Model.LastName<br/>
email:@Model.Email<br/>
City:@Model.City<br/>





我的尝试:



i加载页面时出现以下错误



What I have tried:

i am getting following error while loading page

Sequence contains no elements




An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code





如何显示当前登录的用户记录我已经在谷歌搜索了很多,但只有示例只显示用户ID



how to display current logged in user record i have searched alot on google but there is only on example which displays only userid

推荐答案

我已经完成了我的自我我说谷歌没有这样的解决方案可以显示当前登录用户的所有详细信息



这里我是怎么做的

< br $>
控制器: -





i have done it by my self well as i said there is no such solution available on google for display all details of current logged in users

here is how i did it

controller :-


public ActionResult profile(ApplicationUser a)
      {
          var currentUserId = User.Identity.GetUserId();
          var uinfo = db.Users.FirstOrDefault(d => d.Id == currentUserId);

          if(uinfo!= null)
          {
              return View(uinfo);
          }
          return View("notfound");
      }







并查看: -






and view:-

@model MVCProfile.Models.ApplicationUser

@{
    ViewBag.Title = "profile";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Users Profile</h2>
Firstname:@Model.FirstName<br/>
Lastname:@Model.LastName<br/>
email:@Model.Email<br/>
City:@Model.City<br/>





它对其他人有帮助



it will be helpfull to others


这篇关于如何显示当前登录的用户详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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