如何从数据库中检索用户详细信息并在视图中显示 [MVC 5 应用程序,,标识用户]



我刚开始创建一个mvc 5应用程序,我正在使用默认的Identity用户数据库进行用户数据管理,但是我使用迁移在这个数据库中添加了更多诸如名字,姓氏,电子邮件ID之类的字段,现在我需要在视图页面(作为个人资料页面)中显示所有这些详细信息,但是我不知道如何从默认数据库中检索Identity用户数据,我是一个新的 mvc 开发人员,你能帮我吗

我的模型类

namespace WebApp.Models
{
    public class ExternalLoginConfirmationViewModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }
    }
    public class ManageUserViewModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }
        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }
        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        [System.Web.Mvc.CompareAttribute("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
    public class LoginViewModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }
    public class RegisterViewModel
    {
        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        [Required]
        [DataType(DataType.EmailAddress)]
        public string EmailID { get; set; }
        public int Score { get; set; }
        [Required]
        [Display(Name = "User name")]
        [Remote("CheckUserNameExists", "Common")]
        public string UserName { get; set; }
        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [System.Web.Mvc.CompareAttribute("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
}

DbContextClass

namespace WebApp.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public int Score { get; set; }
    }
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    }

}

从控制器创建类的详细信息视图,并让它搭建基架并将用户详细信息返回到视图中。导入Microsoft.AspNet.Identity以标识当前用户并在 linq 语句中使用它例如

[Authorize()]
ActionResult Details()
{
  var UserId = User.Identity.GetUserId();
  var comp = db.AspNetUsers.Where(i => i.UserName== UserId).First();
  return View(comp);
}

希望对你有帮助

相关内容

  • 没有找到相关文章

最新更新