在这个 ASP.NET MVC 3 应用程序中,我缺少什么来获得不显眼的客户端验证?

发布于 2024-12-02 14:53:37 字数 3071 浏览 0 评论 0原文

我需要帮助弄清楚如何成功地在 ASP.NET MVC 3 应用程序中实现字段的不显眼的客户端验证。我可以看到,不引人注目的客户端验证基本上已启用,因为 MVC 会生成相关的 HTML。

在这种情况下,我想要实现的是在我键入时验证 Bugs 编辑器的输入(即相应的 元素),以用于测试目的我已将属性的最大长度设置为 2。当我测试时,我可以看出当前未进行验证,因此至少缺少某些内容。 因此,此问题的成功标准是对 Bugs 表单字段进行客户端验证

我可以在生成的 HTML 中看到一个可能的问题:Verbose 属性在模型中标记为 Required,但其相应的 例如, 仍然获取 dataval=true 属性, 则获取 Bugs没有。。难道不应该反过来吗,因为具有验证规则的字段应该获取dataval=true,以启用不显眼的验证?

与理解案例相关的代码如下,如果需要更多信息,请告诉我:

Options.cs

public class Options
{
    [Required, StringLength(2)]
    public string Bugs;
    public bool Verbose;
}

Options.cshtml

<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<div id="options-form">
  @using (Html.BeginForm())
  {
    @Html.ValidationSummary(true)
    <fieldset>
      <legend>Options</legend>
      <div class="editor-label">
        @Html.LabelFor(model => model.Bugs)
      </div>
      <div class="editor-field">
        @Html.EditorFor(model => model.Bugs)
        @Html.ValidationMessageFor(model => model.Bugs)
      </div>
      <div class="editor-label">
        @Html.LabelFor(model => model.Verbose)
      </div>
      <div class="editor-field">
        @Html.CheckBoxFor(model => model.Verbose)
        @Html.ValidationMessageFor(model => model.Verbose)
      </div>
   </fieldset>
  }
</div>

两个编辑器(用于 < code>Bugs 和 Verbose) 呈现如下:

<div id="options-form">
<form action="/Options" method="post">
  <fieldset>
    <legend>Options</legend>
      <div class="editor-label">
        <label for="Bugs">Bugs</label>
      </div>
      <div class="editor-field">
        <input class="text-box single-line" id="Bugs" name="Bugs" type="text" value="" />
        <span class="field-validation-valid" data-valmsg-for="Bugs" data-valmsg-replace="true"></span>
      </div>
      <div class="editor-label">
        <label for="Verbose">Verbose</label>
      </div>
      <div class="editor-field">
        <input data-val="true" data-val-required="The Boolean field is required." id="Verbose" name="Verbose" type="checkbox" value="true" /><input name="Verbose" type="hidden" value="false" />
        <span class="field-validation-valid" data-valmsg-for="Verbose" data-valmsg-replace="true"></span>
      </div>
    </fieldset>
  </form>
</div>

I need help figuring out how to succeed in implementing unobtrusive client-side validation of a field in my ASP.NET MVC 3 app. I can see that unobtrusive client-side validation is basically enabled, since MVC generates related HTML.

What I want to achieve in this case is to have validation of input to the Bugs editor (i.e., the corresponding <input> element) as I type, for testing purposes I've set the property's max length to 2. When I test, I can tell that validation does not currently take place, so something is at least missing. So, the success criteria for this question is working client-side validation of the Bugs form field.

I can see one possible problem in the generated HTML: The Verbose property is not marked as Required in the model, but its corresponding <input> still gets the dataval=true attribute for instance, whereas the <input> for Bugs does not. Shouldn't it be the other way around, as fields with validation rules should get dataval=true, to enable unobtrusive validation?

The code that should be relevant to understanding the case follows, please let me know if more info is required:

Options.cs:

public class Options
{
    [Required, StringLength(2)]
    public string Bugs;
    public bool Verbose;
}

Options.cshtml:

<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<div id="options-form">
  @using (Html.BeginForm())
  {
    @Html.ValidationSummary(true)
    <fieldset>
      <legend>Options</legend>
      <div class="editor-label">
        @Html.LabelFor(model => model.Bugs)
      </div>
      <div class="editor-field">
        @Html.EditorFor(model => model.Bugs)
        @Html.ValidationMessageFor(model => model.Bugs)
      </div>
      <div class="editor-label">
        @Html.LabelFor(model => model.Verbose)
      </div>
      <div class="editor-field">
        @Html.CheckBoxFor(model => model.Verbose)
        @Html.ValidationMessageFor(model => model.Verbose)
      </div>
   </fieldset>
  }
</div>

The two editors (for Bugs and Verbose) are rendered as follows:

<div id="options-form">
<form action="/Options" method="post">
  <fieldset>
    <legend>Options</legend>
      <div class="editor-label">
        <label for="Bugs">Bugs</label>
      </div>
      <div class="editor-field">
        <input class="text-box single-line" id="Bugs" name="Bugs" type="text" value="" />
        <span class="field-validation-valid" data-valmsg-for="Bugs" data-valmsg-replace="true"></span>
      </div>
      <div class="editor-label">
        <label for="Verbose">Verbose</label>
      </div>
      <div class="editor-field">
        <input data-val="true" data-val-required="The Boolean field is required." id="Verbose" name="Verbose" type="checkbox" value="true" /><input name="Verbose" type="hidden" value="false" />
        <span class="field-validation-valid" data-valmsg-for="Verbose" data-valmsg-replace="true"></span>
      </div>
    </fieldset>
  </form>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福还没到 2024-12-09 14:53:37

BugsVerbose 是此代码中的公共字段,而不是属性。让它们成为属性,这样就可以解决这个问题。

Bugs and Verbose are public fields in this code, not properties. Make them properties and that should fix it.

段念尘 2024-12-09 14:53:37

BugsVerbose 属性定义 get;set;。这不仅会纠正(添加)验证,而且将来模型绑定器将能够从服务器上的表单字段绑定回模型。

以及关于 Verbose 所需的属性。这是因为需要对值类型进行隐式验证。字符串可以为空,因此如果您没有显式设置 Required 属性,则它不是必需的 - 因此 Bug 会保留为不需要的。但从 C# 的本质来看,bool 不能为 null,因此 mvc 会自动为其添加 Required 属性。你可以通过以下方式控制它

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

Define get; and set; for both Bugs and Verbose properties. That will not just correct(add) validation, but in the future, model binder will be able to bind models back from form fields on server.

And regarding required attribute for Verbose. That is because of implicit required validation for value types taking place. string is nullable, so it is not required if you do not set Required attribute on it explicitely - so Bugs stays without required. But bool cannot be null by nature of c#, so mvc automatically adds Required attribute to it. You can control that with

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
预谋 2024-12-09 14:53:37

我最近遇到了同样的问题(不引人注目的客户端验证在 IE 中不起作用,但在其他浏览器中起作用)。原因在于 jQuery 版本。我从 jQuery 1.6.x 版本回滚到版本 1.5.2,它开始工作。

I experienced the same problem recently (unobtrusive client-side validation didn't work in IE but worked in the other browsers though). The reason was in jQuery version. I rolled back from jQuery 1.6.x version to the version 1.5.2 an it started to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文