0
我無法獲得用戶名和密碼的錯誤消息。我得到驗證錯誤只爲密碼字段不是兩個,如果我通過刪除密碼字段進行測試,然後我看到用戶名字段是必需的錯誤。不知道是什麼問題。以下是它的代碼。在我的模型類的這些字段中具有必需的屬性。視圖和控制器代碼如下所示。只有獲取驗證錯誤的密碼字段,而不是用戶名字段與HTML.ValidateMessageFor
<div class="xmp-form xmp-Contactus container MarginCustom">
<p class="greyfontmod">
Both username and password are <b>case-sensitive.</b>
</p>
<div class="span4" style="margin: 0 auto;float:none">
@using (Html.BeginForm("Submit", "Home", FormMethod.Post))
{
<div class="xmp-form-row">
<label class="xmp-form-label">Username</label>
@Html.TextBoxFor(m => m.UserID, new { @class = "xmp-textbox" })
@Html.ValidationMessageFor(x => x.UserID, " ", new { @class = "greyfontMedError" })
<span class="xmp-validation" style="display:none;">**</span>
</div>
<div class="xmp-form-row">
<label class="xmp-form-label">Password</label>
@Html.PasswordFor(m => m.Password, new { @class = "xmp-Login" })
@Html.ValidationMessageFor(x => x.Password, "", new { @class = "greyfontMedError" })
<span class="xmp-validation" style="display:none;">**</span>
</div>
<p class="greyfontmod">
<b>Note: After 3 unsuccessful attempts your account will be blocked</b>
</p>
<div class="xmp-form-row" style="text-align:center;">
<input type="submit" name="submitBtn" value="SUBMIT" />
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
}
<div class="xmp-form-row" style="text-align:center">
<p class="greyfontmod">
@Html.ActionLink("Forgot Username/Password?", "ResetPassword", "Home", null, new { @class = "Link" })
</p>
</a>
</div>
</div>
控制器
/// <summary>
/// Logins this instance.
/// </summary>
/// <returns>ActionResult</returns>
public ActionResult Submit(CustomerModel customerModel)
{
sessionHelper = new SessionHelper();
if (ModelState.IsValidField("Username") && (ModelState.IsValidField("Password")))
{
customerModel = sessionHelper.ValidateLogin(customerModel);
if (customerModel.BorrowerSeqNo == -1)
{
ModelState.AddModelError("Error", "Invalid Username/Password");
return View("Login", customerModel);
}
return View("AccountStatus", customerModel);
}
else
{
return View("Login", customerModel);
}
}
添加下面我customerModel類。
public class CustomerModel
{
/// <summary>
/// Gets or sets the user identifier.
/// </summary>
/// <value>The user identifier.</value>
[Required]
public string UserID { get; set; }
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>The password.</value>
[Required]
public string Password { get; set; }
/// <summary>
/// Gets or sets the ip address.
/// </summary>
/// <value>The ip address.</value>
你還可以分享你的CustomerModel類嗎? UserID/UserName屬性可能沒有正確的驗證屬性集。 –
請參閱我的編輯 – Blossom
我還沒有嘗試,但它似乎代碼應該工作。您可以嘗試調試並檢查表達式「ModelState.IsValidField(」Password「)」的值。如果這是按預期工作,那麼檢查DOM並查看錯誤是否正在渲染,但由於某些樣式等原因不可見 –