2017-04-14 4 views
0

私のアプリケーションに登録フォームを作成しています。私は一意でなければならないフィールドを持っており、これを保証するカスタム検証が作成されています。固有の値でフォームを送信すると、ページは変更されず、何も送信されません。ApplicationUserのカスタムフィールドと固有フィールド

public JsonResult UserNameInUse(string usernameIn) 
    { 
     return Json(!context.Users.Any(u => u.UserNameIdentity == usernameIn), JsonRequestBehavior.AllowGet); 
    } 

と同じコントローラでの私の非同期レジスタ方法:これまでのところ、私のViewModelは私のコントローラでこの

public class RegisterViewModel 
{ 
    [Required] 
    [CustomValidation(AccountController, "checkifusernametaken", ErrorMessage = "In use already")] 
    [Display(Name = "Username")] 
    [MaxLength(15, ErrorMessage = "This field must be less than 15 characters in length"), MinLength(2, ErrorMessage = "This field must have a value greater than 2")] 
    public string UserNameIdentity { get; set; } 

    [Required] 
    [Display(Name = "First Name")] 
    [MaxLength(15, ErrorMessage = "This field must be less than 15 characters in length"), MinLength(2, ErrorMessage = "This field must have a value greater than 2")] 
    public string FirstName { get; set; } 

    [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; } 
} 

のように見える私はそうのように私のカスタム検証を持って

 // POST: /Account/Register 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser 
      { 
       UserNameIdentity = model.UserNameIdentity, 
       UserName = model.Email, 
       Email = model.Email, 
       FirstName = model.FirstName 
      }; 
      var result = await UserManager.CreateAsync(user, model.Password); 


      if (result.Succeeded) 
      { 
       // add registering users to the default user role 
       UserManager.AddToRole(user.Id, "User"); 

       await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 
       return RedirectToAction("Index", "Home"); 
      } 
      AddErrors(result); 
     } 

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



@model Project.RegisterViewModel 
@{ 
    ViewBag.Title = "Register"; 
} 

<h2>@ViewBag.Title.</h2> 

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{ 
    @Html.AntiForgeryToken() 
    <h4>Create a new account.</h4> 
    <hr /> 
    @Html.ValidationSummary("", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.UserNameIdentity, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.UserNameIdentity, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" class="btn btn-default" value="Register" /> 
     </div> 
    </div> 
} 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 
+0

それがコントローラに到達したときにあなたのモデルがnull?ビューコードも投稿してください。 –

+0

私はカスタム検証を削除するとすぐに、それは正常に動作し、異なるユーザーから一致するusernameIdentity値を受け入れるようにレジスタコントローラを開いたままにします。コントローラーに到達すると、モデルは空ではありません。私はすぐにビューコードを投稿します –

答えて

0

作成します入力されたユーザー名を確認するカスタムValidationAttribute

public class UniqueUsernameAttribute : ValidationAttribute 
{ 
    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     // Get the object 
     var username = value as string; 

     // Check if exists 
     DbContext context = new DbContext(); 
     var user = context.Users.FirstOrDefault(u => u.UserNameIdentity == username) 

     if (user == null) 
      return ValidationResult.Success; 
     else 
      return new ValidationResult("Username already exists"); 
    } 
} 

その後、あなたはそれを使用することができます。

[Required] 
[UniqueUsernameAttribute] 
[Display(Name = "Username")] 
[MaxLength(15, ErrorMessage = "This field must be less than 15 characters in length"), MinLength(2, ErrorMessage = "This field must have a value greater than 2")] 
public string UserNameIdentity { get; set; } 
+0

完璧な、私はあなたの答えを得たとして、この拡張クラスを作成していた –

関連する問題