簡體   English   中英

ValidationMessageFor自動獲取條件

[英]ValidationMessageFor automatically getting fred

我有一個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>
}

動作鏈接按鈕

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

我只是將以下內容添加到_layout,並且有效。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM