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

时间:2014-12-14 08:36:01

标签: c# linq asp.net-mvc-5 asp.net-identity entity-framework-6.1

我刚开始创建一个mvc 5应用程序,我使用默认的Identity用户数据库进行用户数据管理,但我使用迁移向此数据库添加了一些字段,如名字,姓氏,电子邮件ID,现在我需要在视图页面中显示所有细节(作为个人资料页面),但我不知道如何从默认数据库中检索身份用户数据,我是一个新的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")
        {
        }

    }

}

1 个答案:

答案 0 :(得分:1)

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

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

  return View(comp);
}

希望有所帮助