注册按钮没有响应

时间:2012-04-27 16:02:43

标签: c# asp.net-mvc-3 custom-membershipprovider

基本问题:我们目前正在忙于开发C#MVC3 Web应用程序,在编写CustomMembershipProvider和自定义RegisterModel后,我们的Register表单似乎不起作用。这个错误非常令人沮丧。

以下是发生的情况:显示表单,底部有注册按钮:

<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 个答案:

答案 0 :(得分:3)

根据您如何设置所有内容,您可能必须明确告知表单在哪里发布(在您的视图中):

改变这个:

Html.BeginForm()

到:

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

答案 1 :(得分:1)

您是否更改了默认设置的路线?由于路由问题,它可能没有击中控制器?

此外,使用firebug或chrome dev工具的inspect元素功能,并确保表单action =“/ Account / Register”,并且在构建表单时不会做一些奇怪的事情。

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

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