【问题标题】:bootstrap validation error showing before any text entered在输入任何文本之前显示引导验证错误
【发布时间】:2017-06-16 08:45:29
【问题描述】:

我使用带有 MVC 的引导程序并尝试使用验证,但我遇到了一个奇怪的问题。当我使用引导程序打开表单时,文本输入已经用红色标出,并且在我实际在文本框中输入任何内容之前显示验证消息,谁能告诉我如何避免这种情况?这是html

<div class="form-group has-feedback has-error">
            <div class="col-md-4">
                @Html.LabelFor(model => model.Contact.LastName, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-6">
                @Html.TextBoxFor(model => model.Contact.LastName, new { @class = "form-control", @placeholder = "Last Name" })
                @Html.ValidationMessageFor(model => model.Contact.LastName, "You must include a last name.", new { @class = "text-danger" })
            </div>
        </div>

【问题讨论】:

  • 你能分享一下Get Action方法吗,我认为你在Get Action方法中传递了模型名称,例如 public ActionResult Index(ModelName obj) ,使用简单的 public动作结果索引()

标签: asp.net-mvc twitter-bootstrap validation


【解决方案1】:

你的语法:

@Html.ValidationMessageFor(model => model.Contact.LastName, "You must include a last name.", new { @class = "text-danger" })

在此您已经给出错误消息“您必须包含姓氏。”..但它的消息应该来自 数据注释模型验证

[Required(ErrorMessage = "You must include a last name.")]
public string LastName{ get; set; }


 @Html.ValidationMessageFor(model => model.Contact.LastName, string.Empty, new { @class = "text-danger" })

在 ValidationMessageFor 消息中应该为空。如上代码所示。

【讨论】:

  • 我已经尝试过了,但它的引导类似乎是问题所在,一旦我添加“has-error”,文本就会显示为红色并且文本框有一个红色边框在我提交表格之前
猜你喜欢
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 2013-11-29
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
相关资源
最近更新 更多