2016-03-04 21 views
0

我無法獲得用戶名和密碼的錯誤消息。我得到驗證錯誤只爲密碼字段不是兩個,如果我通過刪除密碼字段進行測試,然後我看到用戶名字段是必需的錯誤。不知道是什麼問題。以下是它的代碼。在我的模型類的這些字段中具有必需的屬性。視圖和控制器代碼如下所示。只有獲取驗證錯誤的密碼字段,而不是用戶名字段與HTML.ValidateMessageFor

enter image description here

<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>   
+0

你還可以分享你的CustomerModel類嗎? UserID/UserName屬性可能沒有正確的驗證屬性集。 –

+0

請參閱我的編輯 – Blossom

+0

我還沒有嘗試,但它似乎代碼應該工作。您可以嘗試調試並檢查表達式「ModelState.IsValidField(」Password「)」的值。如果這是按預期工作,那麼檢查DOM並查看錯誤是否正在渲染,但由於某些樣式等原因不可見 –

回答

0

我終於找到了答案。這是我錯過的一件非常愚蠢的事情。下面的線

@Html.ValidationMessageFor(x => x.UserID, " ", new { @class = "greyfontMedError" }) 

實際上已經不是代表空或empty.so正在改寫與空白我的錯誤信息,我無法看到它的報價爲白色的空間。

我知道它是一件愚蠢的事情。

相關問題