如何在 MVC 5 应用程序上更新我的用户帐户数据 ASP.net



我是 asp.net MVC 5身份框架的新手,我正在尝试直接更新我的详细信息。直截了当,我想做的是将我的用户信息更新到数据库中。以前,我使用迁移更改了用户详细信息,并使用实体框架来生成控制器,自行查看和建模。但是,如何更新我的用户详细信息。我见过角色方法..但我永远不明白,我该怎么办?不使用角色..因为我想更新在用户管理控制器中执行此操作所需的所有用户信息...

可能吗? 在不同的控制器中并直接在生成的用户帐户上获取值?那怎么找回呢?

这是我的身份模型

// 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 userFname { get; set; }
        public string userLname { get; set; }
        public string address { get; set; }
        public string userContactNo { get; set; }
        public string commercialName { get; set; }
        public string commercialAddress { get; set; }
        public string commercialEmail { get; set; }
        public string userType { get; set; }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

这是我的注册模型

public class RegisterViewModel
{
    [Required]
    [Display(Name = "User First Name")]
    public string userFname { get; set; }
    [Required]
    [Display(Name = "User Last Name")]
    public string userLname { get; set; }
    [Required]
    [Display(Name = "User Address")]
    public string address { get; set; }
    [Required]
    [Display(Name = "User Contact Number")]
    public string userContactNo { get; set; }
    [Display(Name = "Commercial Name")]
    public string commercialName { get; set; }
    [Display(Name = "Commercial Address")]
    public string commercialAddress { get; set; }
    [EmailAddress]
    [Display(Name = "Commercial Email")]
    public string commercialEmail { get; set; }
    [Key]
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { 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")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
    [Required]        
    public string userType { get; set; }
}

我是如何做到的,

更新

正如他在我下面的回答中评论的那样,他想以相同的方法更新用户列表。所以这将起作用。

[HttpPost]
public async Task<ActionResult> UpdateUserInfo(List<RegisterViewModel> model)
{
  if (!ModelState.IsValid)
    {
        return View(model);
    }
    var userStore = new UserStore<ApplicationUser>(new 
                                  ApplicationDbContext());
    var appManager = new UserManager<ApplicationUser>(userStore);
    // here you can do a foreach loop and get the email and assign new datas
    foreach(var i in model)
     {
       var currentUser = appManager.FindByEmail(i.Email);
       // here you can assign the updated values
       currentUser.userFname = i.userFname;
       // and rest fields are goes here
       await appManager.UpdateAsync(currentUser);
     }
    var ctx = userStore.Context;
    ctx.SaveChanges();
    // now you can redirect to some other method or-else you can return 
    // to this view itself by returning the data
    return RedirectToAction("SomeActionMethod");
}

是的,您应该在视图中拥有字段,并且将有一个@Html.BeginForm和一个submit button来发布您的数据。或者,您可以通过ajax方法发布

希望对您有所帮助。

假设您的ApplicationUser类是实体框架 DBContext 的一部分,您可以使用实体框架像这样检索和更新用户;

var userId = "user id here"; // Set a user ID that you would like to retrieve
var dbContext = new YourDbContext(); // Your entity framework DbContext
// Retrieve a user from the database
var user = dbContext.Set<ApplicationUser>().Find(userId);
// Update a property on your user
user.address = "New value";
// Save the new value to the database
dbContext.SaveChanges();

如果您需要当前登录用户的用户 ID,请使用:

var userId = this.User.Identity.GetUserId();

可以像这样检索和更新多个用户:

var dbContext = new YourDbContext();
// Get all users
var users = dbContext.Set<ApplicationUser>().ToList();
foreach (var user in users)
{
   user.address = "New value";
}
// Save the new value to the database
dbContext.SaveChanges();

实体框架将在您保存时自动跟踪对每个更改。

相关内容

  • 没有找到相关文章

最新更新