2017-04-12 9 views
0

私はasp.net MVC 5 IDフレームワークを初めて使用しています。私は自分の詳細を直接更新しようとしています。 ストレートフォワード、私がしたいのは、自分のユーザー情報をデータベースに更新することです。 以前は、マイグレーションを使用してユーザーの詳細を変更しました。エンティティフレームワークを使用してコントローラを生成し、それを表示してモデル化しました。 ただし、ユーザーの詳細を更新するにはどうすればよいですか。私はロールメソッドを見ましたが、私は理解しません、どうすればいいですか?ロールを使用せずに。 私は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モデルは

あなた ApplicationUserクラスがあなたのEntity FrameworkのDBContextの一部であり、あなたがそうのようなユーザーを検索して更新できると仮定すると
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

この[質問](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

あなたは私の答え@ハムンサンガをチェックしましたか? ?これはあなたが期待しているものです。 –

答えて

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があるでしょう。それ以外の場合はの方法で投稿することができます

希望すると助かります。

+0

あなたも同様にユーザーのリストを取得する方法を教えてください。あなたの答えは完全に私のために働くが、私も同様にユーザーのリストが必要です... ... @ Basanta Matia –

+0

上記の私の更新された答えをチェックして、今あなたのリストを更新することができます...歓迎 –

+1

さて、私は:).............. –

0

です、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(); 

エンティティフレームワークが自動的に追跡されますあなたが保存するときのそれぞれの変更。

私はこれを行う方法
+0

直接IDを渡さないことを意味するユーザーのリストを取得することはできます –

+0

大変お手伝いしてくれてありがとうございます。:) –

+0

問題ありません。 o) – Luke

関連する問題