2016-06-19 6 views
1

これは、アイデンティティWebアプリケーションを持つMVC 5エンティティフレームワークです。私は私のユーザーを作成する私の登録方法を修正しようとしています。私が登録をクリックすると、何も起こりませんし、何のエラーもありません。私はどこで解決策を探し始めるべきかも知らない。MVC5 ID [Register]ボタンをクリックしても何もしません。

enter image description here

AccountsViewModel.cs

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

    public string FirstMidName { get; set; } 

    public string LastName { get; set; } 

    public string UserName { get; set; } 
    [DataType(DataType.Date)] 
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
    public DateTime EnrollmentDate { get; set; } 
    public int DepotID { get; set; } 
    public IEnumerable<SelectListItem> DepotList { get; set; } 
    public IEnumerable<SelectListItem> DepartmentList { get; set; } 

    public int DepartmentID { get; set; } 

} 

AccountController.cs

public class AccountController : Controller 
{ 

    private ApplicationDbContext db = new ApplicationDbContext(); 
    public AccountController()  
    { 
    } 

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager) 
    { 
     UserManager = userManager; 
     SignInManager = signInManager; 
    } 

    private ApplicationUserManager _userManager; 
    public ApplicationUserManager UserManager 
    { 
     get 
     { 
      return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     } 
     private set 
     { 
      _userManager = value; 
     } 
    } 

    // GET: /Account/Register 
    [AllowAnonymous] 
    public ActionResult Register() 
    { 
     RegisterViewModel model = new RegisterViewModel(); 
     ConfigureRegisterViewModel(model); 
     ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName"); 
     ViewBag.DepotID = new SelectList(db.Depots, "DepotID", "DepotName"); 
     return View(model); 
    } 
    // 
    // POST: /Account/Register 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(RegisterViewModel model) 
    { 

     if (!ModelState.IsValid) 
     { 
      ConfigureRegisterViewModel(model); 
      return View(model); 
     } 

     if (ModelState.IsValid) 
     { 

      var user = new ApplicationUser() { 
       UserName = model.UserName, 
       Email = model.Email, 
       FirstMidName = model.FirstMidName, 
       LastName = model.LastName, 
       EnrollmentDate = model.EnrollmentDate, 
       DepotID = model.DepotID, 
       DepartmentID = model.DepartmentID 
      }; 
      var result = await UserManager.CreateAsync(user, model.Password); 
      if (result.Succeeded) 
      { 

       var 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 this link: <a href=\"" + callbackUrl + "\">link</a>"); 
       ViewBag.Link = callbackUrl; 
       return View("DisplayEmail"); 
      } 
      AddErrors(result); 
     } 

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

    private void ConfigureRegisterViewModel(RegisterViewModel model) 
    { 
     IEnumerable<Department> departments = db.Departments.OrderBy(u => u.DepartmentName); 
     model.DepotList = departments.Select(a => new SelectListItem 
     { 
      Value = a.DepartmentID.ToString(), 
      Text = a.DepartmentName.ToString() 
     }); 
     IEnumerable<Depot> depots = db.Depots.OrderBy(u => u.DepotName); 
     model.DepotList = depots.Select(a => new SelectListItem 
     { 
      Value = a.DepotID.ToString(), 
      Text = a.DepotName.ToString() 
     }); 
    } 

}

に画像に示すように、私は、次のプロパティを望ん

Register.cshtml

model RecreationalServicesTicketingSystem.Models.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(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" }) 
     </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"> 
      @Html.LabelFor(m => m.FirstMidName, new { @class = "col-md-2 control-label" }) 
      <div class="col-md-10"> 
       @Html.TextBoxFor(m => m.FirstMidName, new { @class = "form-control" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(m => m.LastName, new { @class = "col-md-2 control-label" }) 
      <div class="col-md-10"> 
       @Html.TextBoxFor(m => m.LastName, new { @class = "form-control" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(m => m.EnrollmentDate, new { @class = "col-md-2 control-label" }) 
      <div class="col-md-10"> 
       @Html.TextBoxFor(m => m.EnrollmentDate, new { @class = "form-control" }) 
      </div> 
     </div> 


      <div class="editor-field"> 
       @using (Html.BeginForm()) 
       { 
        @Html.HiddenFor(m => m.ID) 
        <div class="form-group"> 
         @Html.LabelFor(m => m.DepotID, new { @class = "col-md-2 control-label" }) 
         <div class="col-md-10"> 
          @Html.DropDownListFor(m => m.DepotID, Model.DepotList, "Please select", new { @class = "form-control" }) 
         </div> 
        </div> 
         <div class="form-group"> 
          @Html.LabelFor(m => m.DepartmentID, new { @class = "col-md-2 control-label" }) 
          <div class="col-md-10"> 
           @Html.DropDownListFor(m => m.DepartmentID, Model.DepartmentList, "Please select", new { @class = "form-control" }) 
          </div> 
         </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

私はあなたのコードをテストし、هあなたの問題に気づきました。 次の2つの@using (Html.BeginForm(..))使用:

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 

@using (Html.BeginForm()) 

をし、それらの両方を登録するポストバックaction.this正しくありません。

もう一度は不要です。@using (Html.BeginForm())削除してください。

+0

ありがとうございました!私はそのコードがどのようにそこにあるのか分かりません。 – TykiMikk

+0

@TykiMikkあなたのお役に立てれば、この回答をupvoteしてください。人々はこれがあなたの視点からの正解であり、助けてくれます。 –

0

Register.cshtmlには、誤って入れ子になっているフォームがあるようです。

マークアップのどこかに、メインフォーム内にネストされた@using (Html.BeginForm())があります。

ネスティングを削除すると、問題が解決する場合があります。

関連する問題