2017-12-01 10 views
0

ユーザーがasp.net identity registerViewModelを使用して登録された日時を取得しようとしています。私はそれをAspNetUsersテーブルに追加します。asp.net IDを使用してユーザー登録の現在のDateTimeを取得する

MODEL:

public class RegisterViewModel 
    { 
     public DateTime DateRegistered { 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")] 
     [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
     public string ConfirmPassword { get; set; } 

     [Required] 
     [Display(Name = "User Name")] 
     public string UserName { get; set; } 
    } 

CONTROLLER:

// GET: /Account/Register 
    [HttpGet] 
    [AllowAnonymous] 
    public ActionResult Register() 
    { 
     return View(); 
    } 


    // POST: /Account/Register 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser 
      { 
       UserName = model.UserName 
     }; 

      var db = new ApplicationDbContext(); 
      model.GetDate = DateTime.Now; 
      db.Users.Add(model); 
      db.SaveChanges(); 

      var result = await UserManager.CreateAsync(user, model.Password); 
      if (result.Succeeded) 
      { 
       await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); 

       // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
       string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
       var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
       await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

       return RedirectToAction("Index", "Home"); 
      } 
      AddErrors(result); 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

私はエラーを取得しておいてください。

'Cannot convert from RegisterViewModel to ApplicationUser.' 'RegisterViewModel is not assignable to parameter type ApplicationUser'.

このエラーは、次のコード(モデル)ビットである。

var db = new ApplicationDbContext(); 
model.GetDate = DateTime.Now; 
db.Users.Add(model); 
db.SaveChanges(); 

これに関するお手伝いがあれば助かります。ありがとう

答えて

2
db.Users.Add(model); 

UsersテーブルはSet<ApplicationUser>です。それにはApplicationUser個のオブジェクトを追加することのみが可能です。

Register(RegisterViewModel model) 

modelはタイプRegisterViewModelのパラメータです。これはタイプApplicationUserのパラメータではないため、Usersテーブルに追加することはできません。 user以来

db.Users.Add(user); 

はタイプApplicationUserは次のとおりです。

は、私はあなたがそれにこれを望むものと想定します。一般的なヒントとして


、次のエラーメッセージが表示されたら:

You are trying to use a RegisterViewModel object when you should be using a ApplicationUser object. This does not compute. Use an object of the correct type.

+0

どうもありがとうございましたFlater - 特に明確な説明のために:

Cannot convert from RegisterViewModel to ApplicationUser.

あなたはこのようにお読みください。私は今、このエラーが発生しています - 'datetime2データ型からdatetimeデータ型への変換は、範囲外の値になりました。 ステートメントが終了しました。 – DBoi

+0

@DBoi:これはまったく別の問題です。完全に異なる層で発生します(スポイラーアラート:SQLデータベースのバージョンに関連しています。詳細は記入してください)。これは新しい質問を保証するが、現在の質問は既に答えられている。 – Flater

+0

よろしくお願いします。私はグーグルになるだろう:) – DBoi

関連する問題