2017-04-12 24 views
0

我是新來的asp.net MVC 5身份框架,我嘗試直接更新我的詳細信息。直接轉發,我想要做的是將我的用戶信息更新到數據庫。 以前,我通過使用Migrations更改了用戶詳細信息,並使用實體框架爲了生成我的控制器,查看並自行建模。 但是,如何更新我的用戶詳細信息。我見過角色方法......但我從不明白,我該怎麼辦?沒有使用角色..因爲, 我想要更新UserManageController中所需的所有用戶信息...如何在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; 
     } 
    } 

這裏是我的Registeration模型

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

This [question](http://stackoverflow.com/questions/43362226/how-to-edit-a-user-in-asp-net-identity)看起來與你的相似。 – mmushtaq

+0

請訪問下面的鏈接,它可能會幫助你:http://stackoverflow.com/questions/43362226/how-to-edit-a-user-in-asp-net-identity –

+0

你檢查我的答案@HamunSunga? ?這是你期待的。 –

回答

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.BeginFormsubmit button來發表您的數據。或者 - 你可以通過ajax方法

希望它有幫助。

+0

你能告訴我如何檢索用戶列表以及..你的答案完全適合我,但我想要一個用戶列表,以及那是... ... @ Basanta Matia –

+0

查看我上面更新的答案,你現在可以更新用戶列表...乾杯 –

+1

好吧我會:).............. –

0

假設你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(); 

實體框架會自動追蹤當你保存的時候每個變化。

+0

我可以得到一個用戶列表,我的意思是不通過直接ID –

+0

非常感謝你,你也幫我很多..感謝那...:) –

+0

沒問題。隨意給它一個厚顏無恥的投票:o) – Luke

相關問題