2016-06-19 73 views
1

這是一個身份網絡應用程序的MVC 5實體框架。我正在嘗試修復我的註冊方法,這將創建我的用戶。當我點擊註冊時,什麼都沒有發生,我根本沒有任何錯誤。我甚至不知道從哪裏開始尋找解決方案。MVC5身份點擊註冊按鈕什麼也不做

我想下述性質的所示的圖像中,以

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

我測試你的代碼,並ه注意到你的問題。 您使用兩個@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()),它嵌套在主窗體中。

刪除嵌套可能會解決此問題。