我正在使用模板化的html引导程序主题,并且由于一些奇怪的原因我的提交按钮没有发布。我似乎无法弄清楚为什么。模板以纯HTML形式出现,所以你在这里看到的是我添加了一些MVC。
查看
<!-- Heading -->
@model JDMXCHANGE_SITE_ACCOUNT.ViewModels.AccountController.AuthenticateViewModel
<h2>Registration</h2>
<!-- Sign Up Form Start -->
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-sm-12">
@Html.ValidationMessageFor(P => P.RegistrationViewModel.Name)
@Html.TextBoxFor(P => P.RegistrationViewModel.Name, new { @class = "form-control", placeholder = "Name" })
@*<input type="text" class="form-control" id="name" placeholder="Name">*@
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
@Html.ValidationMessageFor(P => P.RegistrationViewModel.Email)
@Html.TextBoxFor(P => P.RegistrationViewModel.Email, new { @class = "form-control", placeholder = "Email" })
@*<input type="email" class="form-control" id="email" placeholder="Email">*@
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
@Html.ValidationMessageFor(P => P.RegistrationViewModel.ZipCode)
@Html.TextBoxFor(P => P.RegistrationViewModel.ZipCode, new { @class = "form-control", placeholder = "Zip Code" })
@*<input type="text" class="form-control" id="zipCode" placeholder="Zip Code">*@
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
@Html.ValidationMessageFor(P => P.RegistrationViewModel.Username)
@Html.TextBoxFor(P => P.RegistrationViewModel.Username, new { @class = "form-control", placeholder = "Username" })
@*<input type="text" class="form-control" id="username" placeholder="Username">*@
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
@Html.ValidationMessageFor(P => P.RegistrationViewModel.Password)
@Html.PasswordFor(P => P.RegistrationViewModel.Password, new { @class = "form-control", placeholder = "Password" })
@*<input type="password" class="form-control" id="password" placeholder="Password">*@
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="checkbox">
<label>
@Html.ValidationMessageFor(P => P.RegistrationViewModel.AcceptTermsAndConditions)
@Html.CheckBoxFor(P => P.RegistrationViewModel.AcceptTermsAndConditions) Accept terms and conditions
</label>
<span class="pull-right"><a href="#">View</a></span>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="form-group text-center">
<div class="col-sm-12">
<input type="submit" value="Register" name="ActionRegister" class="btn btn-black"/>
<button type="reset" class="btn btn-default">Reset</button>
</div>
</div>
}
视图模型
/// <summary>
/// Holds the registration and login viewmodels since the login/register/contact functionality all reside in the same view. [Authenticate]
/// </summary>
public class AuthenticateViewModel
{
/*NOTE: The classes contained by this class are redundant so that each redundant property has it's own attributes. We do not want to enforce regex on usernames
if they are only loging on and not registering etc.*/
public RegistrationViewModel RegistrationViewModel { get; set; }
public LoginViewModel LoginViewModel { get; set; }
public ContactUsViewModel ContactUsViewModel { get; set; }
public AuthenticateViewModel()
{
this.RegistrationViewModel = new RegistrationViewModel();
this.LoginViewModel = new LoginViewModel();
this.ContactUsViewModel = new ContactUsViewModel();
}
}
public class ContactUsViewModel
{
[DataType(DataType.Text)]
[Required]
public string Name { get; set; }
[DataType(DataType.EmailAddress, ErrorMessage = "Not a valid email address.")]
[Remote("CheckEmailAvailability", "Account", ErrorMessage = "This email already is registered.")]
[Required]
public string Email { get; set; }
[DataType(DataType.MultilineText)]
[Required]
[StringLength(3000, ErrorMessage="Maximum amount of characters allowed is 3000")]
public string Comments {get; set;}
}
public class RegistrationViewModel
{
[DataType(DataType.Text)]
[StringLength(14, MinimumLength = 6, ErrorMessage = "Must be between 6 & 14 characters long.")]
[RegularExpression("^[A-Za-z]{2,}[_-]?[A-Za-z0-9]{2,}$", ErrorMessage = "Only alpha-numeric characters and one '-' or '_' allowed.")]
[Remote("CheckUsernameAvailability", "Account")]
[Required]
public string Username { get; set; }
[DataType(DataType.Password)]
[StringLength(14, MinimumLength=6, ErrorMessage="Must be between 6 & 14 characters long.")]
[Required]
public string Password { get; set; }
[DataType(DataType.Text)]
[Required]
public string Name { get; set; }
[DataType(DataType.EmailAddress, ErrorMessage="Not a valid email address.")]
[Remote("CheckEmailAvailability", "Account", ErrorMessage="This email already is registered.")]
[Required]
[EmailAddress(ErrorMessage="Not a valid email address.")]
public string Email { get; set; }
[DataType(DataType.PostalCode, ErrorMessage="Invalid zip code.")]
[Display(Name="Zip code")]
[Required]
[RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid zip code.")]
public string ZipCode { get; set; }
[Display(Name="Accept terms and conditions")]
[Required(ErrorMessage="You must accept the terms and conditions.")]
public bool AcceptTermsAndConditions { get; set; }
}
public class LoginViewModel
{
[DataType(DataType.Text)]
[Required]
public string Username { get; set; }
[DataType(DataType.Password)]
[Required]
public string Password { get; set; }
[Display(Name="Remember me")]
public bool RememberMe { get; set; }
}
行动
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(ViewModels.AccountController.AuthenticateViewModel _model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string 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 <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
奇怪的是,如果我没有在表单中输入任何内容并尝试提交,我会为每个输入字段获取所有正确的验证消息。如果我输入所有必需的数据并点击提交,则没有任何反应。我嗅到网络,我看到我的远程验证工作正常,但当我点击提交按钮时,网络嗅探器上没有任何显示。此外,如果我尝试提交表单而不选中“同意条款和条件”复选框,我不会收到有关它的验证消息,我应该得到它。上次我的帖子失败的原因是某些模型绑定错误无法正常失败,但我似乎也看不到任何可能的错误来源。