如何检索视图从数据库并显示用户的详细信息[MVC 5应用程序,,用户身份] [英] How to retrieve user details from database and display in a view [MVC 5 application,,Identity User]

查看:143
本文介绍了如何检索视图从数据库并显示用户的详细信息[MVC 5应用程序,,用户身份]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始创建一个MVC 5应用程序,我使用的用户数据管理的默认用户身份的数据库,但是我用的迁移添加像姓,名,电子邮件ID一些领域这个数据库,现在我需要显示这一切详细视图页面(如个人资料页),但我不知道如何从默认的数据库中获取用户身份的数据,我是一个新的MVC开发人员可以帮我。

I just started creating an mvc 5 application ,I'm using default Identity user database for user data management,But I added some more fields like First Name ,Last Name ,Email Id to this database using migration,Now I need to display this all detail in a view page (as a profile page) ,but I don't know how to retrieve Identity user data from default database ,I am a new mvc developer can you help me

我的模型类

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语句中使用它
例如。

Create a detail view of your class from the controller and let it scaffold and return user detail to your view. Import Microsoft.AspNet.Identity to identify the current user and use it in your linq statement E.g.

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

  return View(comp);
}

希望它能帮助

这篇关于如何检索视图从数据库并显示用户的详细信息[MVC 5应用程序,,用户身份]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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