我有密码并确认带有比较属性的密码字段,但它有问题吗?尝试通过NuGet包管理器更新所有内容,即使我输入'123456',密码仍然不匹配,我想知道该怎么办?
public class AccountsViewModel
{
public class Register
{
[Required]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Compare("Password")]
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
}
}
控制器
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "id,username,password")] AccountsViewModel.Register viewModel)
{
if (ModelState.IsValid)
{
account account = new account();
db.accounts.Add(account).username = viewModel.Username;
db.accounts.Add(account).password = viewModel.Password;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(viewModel);
}
查看
@model trainingmvc.Models.AccountsViewModel.Register
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Accounts", 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(m => m.Username, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Username, 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">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Web Config
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
捆绑
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
答案 0 :(得分:1)
您的POST方法具有[Bind]
属性
public async Task<ActionResult> Create([Bind(Include = "id,username,password")] AccountsViewModel.Register viewModel)
从绑定中排除模型ConfirmPassword
属性,因此它的值null
无效(它与Password
的值不匹配)所以{{1}错误被添加。
ModelState
是一个视图模型,视图模型永远不需要AccountsViewModel
属性(因为视图模型只包含在视图中编辑的属性)。只需删除该属性即可绑定所有属性,并且您的模型将有效。