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

Asp.net mvc 如何在ASP.net MVC 5应用程序上更新我的用户帐户数据,asp.net-mvc,entity-framework,asp.net-identity,Asp.net Mvc,Entity Framework,Asp.net Identity,我是asp.NETMVC5身份框架的新手,我正在尝试直接更新我的详细信息。 直截了当地说,我想做的是将我的用户信息更新到数据库中。 以前,我使用迁移更改了我的用户详细信息,并使用实体框架来生成我的控制器、视图和模型。 但是,如何更新我的用户详细信息。我见过角色扮演法,但我不明白,我该怎么做?不使用角色..因为, 我想更新在UserManageController中执行此操作所需的所有用户信息 可能吗?在不同的控制器中,直接在生成的用户帐户上获取值?那怎么取回呢 这是我的身份模型 // You c

我是asp.NETMVC5身份框架的新手,我正在尝试直接更新我的详细信息。 直截了当地说,我想做的是将我的用户信息更新到数据库中。 以前,我使用迁移更改了我的用户详细信息,并使用实体框架来生成我的控制器、视图和模型。 但是,如何更新我的用户详细信息。我见过角色扮演法,但我不明白,我该怎么做?不使用角色..因为, 我想更新在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;
        }
    }
假设ApplicationUser类是Entity Framework DBContext的一部分,您可以使用Entity Framework检索和更新这样的用户

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();
可以按如下方式检索和更新多个用户:

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();
实体框架将在您保存时自动跟踪对每个的更改。

我是如何做到的

更新:正如他在我下面的回答中所评论的,他想用同样的方法更新一个用户列表。因此,这将起作用

[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和一个提交按钮来发布您的数据。或者您可以通过ajax方法发布


希望有帮助。

这看起来与您的类似。请访问下面的链接,它可能会帮助您:您是否检查了我的答案@hamunnga??这正是你所期待的。可能是@Basanta Matia-yeh的副本,但我希望直接访问,而不是使用不同的模型…我在问,我能得到自动生成的模型类以生成我的编辑吗?有可能吗?我可以得到一份用户名单吗?我的意思是不通过直接ID。非常感谢你,你也帮了我很多。谢谢…:没问题。请随便给它一个厚颜无耻的投票:o你能告诉我如何检索用户列表吗..你的答案对我来说非常合适,但我也想要一个用户列表那就是……@Basanta MatiaCheck我上面更新的答案,你现在可以更新用户列表了…干杯
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();
[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");
}