ValidationMessageFor自动获取fred

时间:2014-12-22 06:14:59

标签: c# validation asp.net-mvc-5 unobtrusive-javascript

我有一个actionmethod resetpassword类型为get,它返回一个视图。该方法从actionlink按钮调用。对于这个视图,我传递了一个用户obj。现在,当我点击actionlink时,它会转到视图,但是当我应用validationfor时,验证会在加载视图时自动触发。这是因为我将一个用户的obj传递给视图。如果是这种情况,那么我如何为该操作方法关闭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")

发布方法

[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 个答案:

答案 0 :(得分:1)

我刚刚将以下内容添加到_layout并且它有效。

@Scripts.Render("~/bundles/jqueryval")