【问题标题】:.input-validation-error class not added to input in view.input-validation-error 类未添加到视图中的输入
【发布时间】:2009-09-14 14:24:15
【问题描述】:

我正在尝试向我的 asp.net mvc 表单添加一些简单的验证,但无法将 .input-validation-error 类添加到我的输入中。验证摘要错误和 .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);
}

【问题讨论】:

    标签: c# asp.net-mvc validation html-helper


    【解决方案1】:

    在我看来,AddModelError id 参数应该与输入的 id 匹配。所以在你的情况下,它应该改为:

    ModelState.AddModelError("txtEmailOrDealerID", "E-Mail Address or Dealer ID is required.");    
    

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 2019-09-19
      • 2021-11-06
      • 1970-01-01
      相关资源
      最近更新 更多