【发布时间】:2015-12-07 12:07:46
【问题描述】:
我有密码并使用比较属性确认密码字段,但它有问题?尝试通过 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")
}
网页配置
<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"));
【问题讨论】:
-
关闭客户端验证,在回发的时候绑定
Password和ConfirmPassword属性看看设置了什么。 -
很抱歉,我该怎么做?我对网络开发有点陌生。
-
它是否真的进入了服务器代码,还是在您调试时在客户端被拒绝?如果它到达服务器,可能是因为您没有绑定确认密码字段。尝试使 [Bind(Include = "id,username,password")] 这个 [Bind(Include = "id,username,password,confirmpassword")]
-
我在我的控制器上设置了一个断点,但它没有达到那个断点,所以我猜它在客户端失败了。
-
嗯。我的设置基本相同,没有问题。如果删除 datatype 属性并验证内容是否相同,是否仍会导致问题?
标签: javascript c# asp.net-mvc-5 entity-framework-6