【问题标题】:ValidationMessageFor automatically getting fredValidationMessageFor 自动获取 fred
【发布时间】:2015-02-20 05:46:13
【问题描述】:

我有一个actionmethodresetpassword,它的类型是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 按钮

<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; }

【问题讨论】:

  • @Html.ActionLink("Reset Password", "ResetPassword") 没有传递任何东西给public ActionResult ResetPassword(UserBE user) 只需删除参数并在方法中初始化一个新的UserBE
  • @StephenMuecke:我做到了。现在我没有得到自动验证,但现在第二个输入密码没有得到验证。如果我将其留空,则不会给出任何验证错误消息。
  • 抱歉,不明白 - 这将与您的 POST 方法相关联,而不是 GET 方法。你能发布你的 POST 方法和模型,显示你应用了哪些验证属性
  • @StephenMuecke:请看我的编辑。
  • 不知道为什么不显示验证消息,但是您的 POST 方法应该以 if(!ModelState.IsValid) { return View(user); } 开头,因此在您尝试更新视图之前,视图会返回到更正错误。此外,您的错误消息与属性 ConfirmedPassword 混淆了

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


【解决方案1】:

我刚刚在 _layout 中添加了以下内容,它起作用了。

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

【讨论】:

  • 这意味着您在查看脚本时遇到了问题。确保删除您在问题中指出的那些
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多