这是非常基本的,但在比较验证时总是返回false。还有其他人遇到过这个问题吗?
public class UsersRegisterUserViewModel
{
[DisplayName("E-Mail Address")]
[Required(ErrorMessage = "E-Mail Address is required")]
[RegularExpression(@"^[A-Za-z0-9_\-\.]+@(([A-Za-z0-9\-])+\.)+([A-Za-z\-])+$", ErrorMessage = "Invalid E-mail Address")]
public string RegUsername { get; set; }
[Required]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "Passwords must match")]
[DataType(DataType.Password)]
public string RegConfirmPassword { get; set; }
}
答案 0 :(得分:1)
adapters.add("equalto", ["other"], function (options) {
var prefix = getModelPrefix(options.element.name),
other = options.params.other,
fullOtherName = appendModelPrefix(other, prefix),
//element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];
element = $(options.form).find(":input[name='" + fullOtherName + "']")[0];
答案 1 :(得分:1)
独立于帐户控制器比较密码时,MVC3比较属性是错误的。它似乎是硬编码只与帐户控制器一起工作 1.将来自RegisterModel的电子邮件,密码和确认密码剪切并通过名为ViewModels / ShortRegister.cs的新文件 2.从注册视图中剪切剃刀代码(电子邮件,密码,确认密码)并将其过去进入部分视图,将其命名为“_shortRegistration”。 3.创建一个名为“ShortRegistration”的新控制器。将部分视图添加到ShortRegistation中。 5.添加相关的jquery脚本
此错误是否已修复?我禁用了Compare属性并编写了jquery和CCS来解决这个问题!我非常乐意通过电子邮件发送代码来证明Compare是错误的。
答案 2 :(得分:0)
型号:
public class UsersRegisterUserViewModel
{
[DisplayName("E-Mail Address")]
[Required(ErrorMessage = "E-Mail Address is required")]
[RegularExpression(@"^[A-Za-z0-9_\-\.]+@(([A-Za-z0-9\-])+\.)+([A-Za-z\-])+$", ErrorMessage = "Invalid E-mail Address")]
public string RegUsername { get; set; }
[Required]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "Passwords must match")]
[DataType(DataType.Password)]
public string RegConfirmPassword { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new UsersRegisterUserViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(UsersRegisterUserViewModel model)
{
return View(model);
}
}
查看:
@model UsersRegisterUserViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(x => x.RegUsername)
@Html.EditorFor(x => x.RegUsername)
@Html.ValidationMessageFor(x => x.RegUsername)
</div>
<div>
@Html.LabelFor(x => x.Password)
@Html.EditorFor(x => x.Password)
@Html.ValidationMessageFor(x => x.Password)
</div>
<div>
@Html.LabelFor(x => x.RegConfirmPassword)
@Html.EditorFor(x => x.RegConfirmPassword)
@Html.ValidationMessageFor(x => x.RegConfirmPassword)
</div>
<input type="submit" value="OK" />
}
所以现在问题变成了:你的代码与我的代码有什么不同?