提问人:user2998990 提问时间:12/22/2014 最后编辑:user2998990 更新时间:12/22/2014 访问量:418
ValidationMessage用于自动获取 fred
ValidationMessageFor automatically getting fred
问:
我有一个 get 类型,它返回一个视图。该方法从按钮调用。对于这个视图,我正在传递一个用户 obj。现在,当我单击操作链接时,它会转到视图,但是当我应用时,验证会在加载视图时自动触发。这是因为我正在将用户的对象传递给视图吗?如果是这种情况,那么我该如何关闭该操作方法的验证,因为我只想加载输入,并且当用户开始填充输入时,应该只触发验证。actionmethod
resetpassword
actionlink
validationfor
HttpGet
操作方法。
[ValidateInput(false)]
[HttpGet]
[ActionName("ResetPassword")]
public ActionResult ResetPassword(UserBE user)
{
user.Email = TempData["userEmail"].ToString();
return View(user);
}
视图
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
@model XYZ.BE.UserBE
@{
ViewBag.Title = "ResetPassword";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ResetPassword</h2>
@using (Html.BeginForm("ResetPassword", "User"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model=>model.Email)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
@Html.HiddenFor(model=>model.Email)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NewPassword, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(model => model.NewPassword)
@Html.ValidationMessageFor(model => model.NewPassword)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ConfirmedPassword, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(model => model.ConfirmedPassword)
@Html.ValidationMessageFor(model => model.ConfirmedPassword)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Reset Password" class="btn btn-default" />
</div>
</div>
}
ActionLink BUtton
<h3>@Html.ActionLink("Reset Password", "ResetPassword")
Post 方法
[HttpPost]
[ActionName("ResetPassword")]
public ActionResult ResetPasswordPost(UserBE user)
{
user = UserBL.AuthenticateUser(user);
if (!user.AuthenticUser || (user.Password==user.NewPassword))
{
return View(user);
}
else
{
return UserBL.ResetPassword(user)?View("LoginSuccessful",user):View(user);
}
}
型
[Required(ErrorMessage = "Password is required")]
public string Password { get; set; }
private bool authenticUser = false;
public bool AuthenticUser
{
get { return authenticUser; }
set { authenticUser = value; }
}
[Required(ErrorMessage = "Password is required")]
public string NewPassword { get; set; }
[Required(ErrorMessage = "Confirm passord and NewPassWord does not match")]
[Compare("NewPassword")]
public string ConfirmedPassword { get; set; }
答:
1赞
user2998990
12/22/2014
#1
我只是将以下内容添加到_layout中,它起作用了。
@Scripts.Render("~/bundles/jqueryval")
评论
1赞
12/22/2014
这意味着您看到的脚本有问题。确保删除问题中指出的那些
评论
@Html.ActionLink("Reset Password", "ResetPassword")
没有传递任何东西 只需删除参数并在方法中初始化一个新的public ActionResult ResetPassword(UserBE user)
UserBE
if(!ModelState.IsValid) { return View(user); }
ConfirmedPassword