我是asp.net MVC 5身份框架的新手,我试着直接更新我的详细信息。 直截了当,我想要做的是将我的用户信息更新到数据库。 以前,我通过使用迁移更改了我的用户详细信息,并使用实体框架来生成我的控制器,自行查看和建模。 但是,如何更新用户详细信息。我见过角色方法..但我从来不明白,我该怎么办?没有使用角色..因为, 我想在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; }
}
答案 0 :(得分:0)
假设您的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();
如果您需要当前登录用户的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();
实体框架会在您保存时自动跟踪每个更改。
答案 1 :(得分:0)
我是怎么做到的,
更新:正如他在下面的回答中所评论的那样,他希望以相同的方法更新用户列表。所以这会奏效。
[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
方法
希望它有所帮助。