【问题标题】:How to update my user account data on ASP.net MVC 5 application如何在 ASP.net MVC 5 应用程序上更新我的用户帐户数据
【发布时间】:2017-04-12 09:14:29
【问题描述】:

我是 asp.net MVC 5 身份框架的新手,我正在尝试直接更新我的详细信息。 直截了当,我想做的是将我的用户信息更新到数据库中。 以前,我使用 Migrations 更改了我的用户详细信息,并使用实体框架来生成我的控制器、视图和模型。 但是,如何更新我的用户详细信息。我看过角色方法..但我不明白,我该怎么办?不使用角色..因为, 我想在 UserManageController 中更新所有需要的用户信息...

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

这是我的身份模型

// 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; }

}

【问题讨论】:

  • 这个question 看起来和你的很像。
  • 请访问以下链接,它可能对您有所帮助:stackoverflow.com/questions/43362226/…
  • 你检查了我的答案@HamunSunga 吗?这是你所期待的。
  • @Basanta Matia - 是的,但我想要直接访问,而不是使用不同的模型......我问,我可以获取自动生成的模型类以生成我的编辑吗?有可能吗?

标签: asp.net-mvc entity-framework asp.net-identity


【解决方案1】:

我是怎么做到的,

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

[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方法发帖

希望对你有帮助。

【讨论】:

  • 能否请您告诉我如何检索用户列表......马蒂亚
  • 查看我上面更新的答案,您现在可以更新用户列表...干杯
【解决方案2】:

假设您的 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();

如果你需要当前登录用户的userId,使用:

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();

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

【讨论】:

  • 我可以得到一个用户列表,我的意思是不直接传递 id
  • 非常感谢你也帮了我很多..谢谢你...:)
  • 没问题。随意给它一个厚脸皮的投票:o)
猜你喜欢
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 2020-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多