1
我想添加一些簡單的驗證到我的asp.net mvc窗體,並且無法將.input驗證錯誤類添加到我的輸入。 validation-summary-errors和.field-validation-error正常工作。在此先感謝您的幫助!。輸入驗證錯誤類未添加到視圖中的輸入
編輯:謝謝大家的幫助!我不得不加入這一行的控制,以避免錯誤:
ModelState.SetModelValue("txtEmailOrDealerID", collection.ToValueProvider()["txtEmailOrDealerID"]);
的觀點:
<%using (Html.BeginForm("DealerLogin", "Home", FormMethod.Post))
{ %>
<fieldset>
<legend>Dealer Login</legend>
<div class="row">
<%=Html.Label("txtEmailOrDealerID", "E-Mail Or Dealer ID:")%>
<%=Html.TextBox("txtEmailOrDealerID")%>
<%=Html.ValidationMessage("txtEmailOrDealerID", "*")%>
</div>
<div class="row">
<%=Html.Label("txtPassword", "Password:")%>
<%=Html.Password("txtPassword")%>
<%=Html.ValidationMessage("txtPassword", "*")%>
</div>
<div class="centerbutton">
<input type="submit" id="btnSubmitDealer" value="Login"/>
</div>
</fieldset>
<%} %>
控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DealerLogin(FormCollection collection)
{
if (string.IsNullOrEmpty(collection["txtEmailOrDealerID"].Trim()))
ModelState.AddModelError("txtEmailOrDealerID", "E-Mail Address or Dealer ID is required.");
if (string.IsNullOrEmpty(collection["txtPassword"].Trim()))
ModelState.AddModelError("txtPassword", "Password is required.");
if (ModelState.IsValid)
return Redirect("~/MyUploads");
else
return View("Index");
}
的CSS:
/*Validation*/
.field-validation-error{color: #ff0000;}
.input-validation-error{border: 1px solid #ff0000; background-color: #ffeeee;}
.validation-summary-errors{color: #ff0000;}
HTML.Label擴展方法:
public static string Label(this HtmlHelper helper, string forControl, string text)
{
return String.Format("<label for='{0}'>{1}</label>", forControl, text);
}