2012-04-27 78 views
1

基本问题:我们目前正在忙于开发一个C#MVC3 web应用程序,并且在编写CustomMembershipProvider和一个自定义RegisterModel后,我们的注册表单似乎没有工作。这个错误非常令人沮丧。注册按钮不响应

这里发生了什么:表显示,与寄存器底部按钮:

<input type="submit" value="Register" /> 

但是,当您点击注册按钮,没有任何反应。 这里的HttpPost方法:

[HttpPost] 
    public ActionResult Register(RegisterModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      // Attempt to register the user 
      MembershipCreateStatus createStatus = ((CustomMembershipProvider)Membership.Provider).CreateUser(model); 

      if (createStatus == MembershipCreateStatus.Success) 
      { 
       if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
       { 
        return Redirect(returnUrl); 
       } 
       else 
       { 
        return RedirectToAction("Index", "Home"); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("","Something went wrong"); 
      } 
     } 

     // If we got this far, something failed, redisplay form 

     return View(model); 
    } 

有什么想法?任何帮助将不胜感激。

编辑:为你的 “观看” 游 - 这里的完整视图

@model PMES.Models.RegisterModel 

@{ 
ViewBag.Title = "Register"; 
} 

<h2>Create a New Account</h2> 
<p> 
Use the form below to create a new account. 
</p> 
<p> 
Passwords are required to be a minimum of 
@Membership.MinRequiredPasswordLength   characters in length. 
</p> 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">   </script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.") 
<div> 
    <fieldset> 
     <legend>Account Information</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(m => m.Email) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(m => m.Email) 
      @Html.ValidationMessageFor(m => m.Email) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(m => m.Password) 
     </div> 
     <div class="editor-field"> 
      @Html.PasswordFor(m => m.Password) 
      @Html.ValidationMessageFor(m => m.Password) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(m => m.ConfirmPassword) 
     </div> 
     <div class="editor-field"> 
      @Html.PasswordFor(m => m.ConfirmPassword) 
      @Html.ValidationMessageFor(m => m.ConfirmPassword) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(m => m.Name) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(m => m.Name) 
      @Html.ValidationMessageFor(m => m.Name) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(m => m.FirstName) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(m => m.FirstName) 
      @Html.ValidationMessageFor(m => m.FirstName) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(m => m.Mobile) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(m => m.Mobile) 
      @Html.ValidationMessageFor(m => m.Mobile) 
     </div> 
     <p> 
      <input type="submit" value="Register" /> 
     </p> 
    </fieldset> 
</div> 
} 

为了获得更大的观赏乐趣:

namespace PMES.Controllers 
{ 
public class AccountController : Controller 
{ 
    private IUserRepository userRepository; 
    // 
    // GET: /Account/LogOn 
    public AccountController() 
    { 
     ProjectManagementContext context = new ProjectManagementContext(); 
     this.userRepository = new UserRepository(context); 
    } 

    /*public AccountController(IUserRepository userRepository) 
    { 
     this.userRepository = userRepository; 
    }*/ 

    public ActionResult LogOn() 
    { 
     return View(); 
    } 

    // 
    // POST: /Account/LogOn 

    [HttpPost] 
    public ActionResult LogOn(LogOnModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      if (Membership.ValidateUser(model.Email, model.Password)) 
      { 
       FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe); 
       if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
       { 
        return Redirect(returnUrl); 
       } 
       else 
       { 
        return RedirectToAction("Index", "Home"); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("", "The user name or password provided is incorrect."); 
      } 
     } 

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

    // 
    // GET: /Account/LogOff 

    public ActionResult LogOff() 
    { 
     FormsAuthentication.SignOut(); 

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

    // 
    // GET: /Account/Register 

    public ActionResult Register() 
    { 
     return View(); 
    } 

    // 
    // POST: /Account/Register 

    [HttpPost] 
    public ActionResult Register(RegisterModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      // Attempt to register the user 
      MembershipCreateStatus createStatus = ((CustomMembershipProvider)Membership.Provider).CreateUser(model); 

      if (createStatus == MembershipCreateStatus.Success) 
      { 
       if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
       { 
        return Redirect(returnUrl); 
       } 
       else 
       { 
        return RedirectToAction("Index", "Home"); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("","Something went wrong"); 
      } 
     } 

     // If we got this far, something failed, redisplay form 

     return View(model); 
    } 

    // 
    // GET: /Account/ChangePassword 

    [Authorize] 
    public ActionResult ChangePassword() 
    { 
     return View(); 
    } 

    // 
    // POST: /Account/ChangePassword 

    [Authorize] 
    [HttpPost] 
    public ActionResult ChangePassword(ChangePasswordModel model) 
    { 
     if (ModelState.IsValid) 
     { 

      // ChangePassword will throw an exception rather 
      // than return false in certain failure scenarios. 
      bool changePasswordSucceeded; 
      try 
      { 
       MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */); 
       changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword); 
      } 
      catch (Exception) 
      { 
       changePasswordSucceeded = false; 
      } 

      if (changePasswordSucceeded) 
      { 
       return RedirectToAction("ChangePasswordSuccess"); 
      } 
      else 
      { 
       ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); 
      } 
     } 

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

    // 
    // GET: /Account/ChangePasswordSuccess 

    public ActionResult ChangePasswordSuccess() 
    { 
     return View(); 
    } 

    public JsonResult checkEmail(String email) 
    { 
     var result = Membership.FindUsersByEmail(email).Count == 0; 
     return Json(result, JsonRequestBehavior.AllowGet); 
    } 

    #region Status Codes 
    private static string ErrorCodeToString(MembershipCreateStatus createStatus) 
    { 
     // See http://go.microsoft.com/fwlink/?LinkID=177550 for 
     // a full list of status codes. 
     switch (createStatus) 
     { 
      case MembershipCreateStatus.DuplicateUserName: 
       return "User name already exists. Please enter a different user name."; 

      case MembershipCreateStatus.DuplicateEmail: 
       return "A user name for that e-mail address already exists. Please enter a different e-mail address."; 

      case MembershipCreateStatus.InvalidPassword: 
       return "The password provided is invalid. Please enter a valid password value."; 

      case MembershipCreateStatus.InvalidEmail: 
       return "The e-mail address provided is invalid. Please check the value and try again."; 

      case MembershipCreateStatus.InvalidAnswer: 
       return "The password retrieval answer provided is invalid. Please check the value and try again."; 

      case MembershipCreateStatus.InvalidQuestion: 
       return "The password retrieval question provided is invalid. Please check the value and try again."; 

      case MembershipCreateStatus.InvalidUserName: 
       return "The user name provided is invalid. Please check the value and try again."; 

      case MembershipCreateStatus.ProviderError: 
       return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator."; 

      case MembershipCreateStatus.UserRejected: 
       return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator."; 

      default: 
       return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."; 
     } 
    } 
    #endregion 
} 

}

+2

您的提交按钮是否在一个'form'标签内? – Brandon 2012-04-27 16:03:42

+0

是的,它确实是^^ – SkyronWtr 2012-04-27 16:06:41

+0

那么你的'Register'动作被调用了吗? – 2012-04-27 16:07:00

回答

3

根据您所拥有的一切设置你可能必须明确告诉形式在哪里发表(在您看来):

更改此:

Html.BeginForm() 

到:

Html.BeginForm("Register", "YourController") 
+0

试过了,但没有带来解决方案:( – SkyronWtr 2012-04-27 16:21:05

+1

@SkyonWtr就像上面提到的那样,我会下载Fiddler http://fiddler2.com/fiddler2/来查看请求的实际发送位置,我仍然会下注这是一个控制器/视图不匹配 – AGoodDisplayName 2012-04-27 16:30:20

+0

安装,因为我们说^^ – SkyronWtr 2012-04-27 16:32:22

1

你从默认设置改变了你的路线?由于路由问题,它可能不会触及控制器?

此外,使用萤火虫或铬开发工具的inspect元素功能,并确保形式action =“/帐户/注册”,并且它正在构建表单时不奇怪。

有时会发生奇怪的事情,它不会正确构建表单。

此外,您可以使用chrome dev工具中的Network选项卡来查看当您点击提交按钮时发布到服务器的内容,并且您可以看到服务器响应。

+0

谢谢,经过一番捣鼓之后,我注意到当我发布表单时,它将我发送到/ AccountController/Register,在那里我认为它应该是/帐户/注册 - 我该如何改变? – SkyronWtr 2012-04-28 11:29:20

+0

我已经想通了,谢谢! – SkyronWtr 2012-04-28 11:39:31

+0

很高兴你明白了。如果您觉得我的回答回答了您的问题,请将其标记为未来人员的答案。谢谢 :) – 2012-04-28 18:34:59