ValidationMessage用于自动获取 fred

ValidationMessageFor automatically getting fred

提问人:user2998990 提问时间:12/22/2014 最后编辑:user2998990 更新时间:12/22/2014 访问量:418

问:

我有一个 get 类型,它返回一个视图。该方法从按钮调用。对于这个视图,我正在传递一个用户 obj。现在,当我单击操作链接时,它会转到视图,但是当我应用时,验证会在加载视图时自动触发。这是因为我正在将用户的对象传递给视图吗?如果是这种情况,那么我该如何关闭该操作方法的验证,因为我只想加载输入,并且当用户开始填充输入时,应该只触发验证。actionmethodresetpasswordactionlinkvalidationforHttpGet

操作方法。

[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; }
C# 验证 ASP.NET-MVC-5 不显眼的 JavaScript

评论

0赞 12/22/2014
@Html.ActionLink("Reset Password", "ResetPassword")没有传递任何东西 只需删除参数并在方法中初始化一个新的public ActionResult ResetPassword(UserBE user)UserBE
0赞 user2998990 12/22/2014
@StephenMuecke : 我做到了。现在我没有得到自动验证,但现在第二个输入密码没有得到验证。如果我将其留空,它仍然不会给出任何验证错误消息。
0赞 12/22/2014
对不起,不明白 - 这将与您的 POST 方法相关联,而不是 GET 方法。您能否发布 POST 方法和模型,显示您应用了哪些验证属性
0赞 12/22/2014
无法发现为什么不显示验证消息,但您的 POST 方法应以 开头,以便在您尝试更新视图之前返回到更正错误。此外,您的错误消息在属性上混淆了if(!ModelState.IsValid) { return View(user); }ConfirmedPassword
0赞 user2998990 12/22/2014
@StephenMuecke : 好的,谢谢,我会尝试找出为什么第一次验证没有触发。您能否向我解释为什么当我在操作方法中创建对象时自动验证停止了。

答:

1赞 user2998990 12/22/2014 #1

我只是将以下内容添加到_layout中,它起作用了。

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

评论

1赞 12/22/2014
这意味着您看到的脚本有问题。确保删除问题中指出的那些