在这种情况下,为什么 ASP.NET MVC 3 不启用客户端验证?

发布于 2024-12-02 08:34:54 字数 2045 浏览 1 评论 0 原文

我正在尝试将不引人注目的客户端验证集成到我的 ASP.NET MVC 3 项目中,按照 布拉德·威尔逊的食谱。但是,它不会在渲染视图中启用。例如,我的 元素(即编辑器字段)未按规定接收 data-val 属性。

我已执行以下操作来启用不显眼的客户端验证:

Web.config:

<configuration>
  <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
</configuration>

Options.cs:

public class Options
{
    // Annotate with validation rules, in order to generate client-side validation code 
    [Required, StringLength(60)]
    public string Bugs = "";
}

_Layout.cshtml:

<head>
  <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>
</head>

Options .cshtml

@model MyProject.Models.Options

<div id="options-form">
  @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>
  </fieldset>
</div>

为编辑器字段生成此 HTML:

<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="" />

如您所见,没有 data-val 属性:(

I'm trying to integrate unobtrusive client-side validation in my ASP.NET MVC 3 project, as per Brad Wilson's recipe. However, it does not get enabled in the rendered view. For example, my <input> elements (i.e., editor fields) do not receive a data-val attribute as prescribed.

I have done the following to enable unobtrusive client-side validation:

Web.config:

<configuration>
  <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
</configuration>

Options.cs:

public class Options
{
    // Annotate with validation rules, in order to generate client-side validation code 
    [Required, StringLength(60)]
    public string Bugs = "";
}

_Layout.cshtml:

<head>
  <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>
</head>

Options.cshtml:

@model MyProject.Models.Options

<div id="options-form">
  @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>
  </fieldset>
</div>

This HTML gets generated for the editor field:

<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="" />

As you can see, no data-val attribute :(

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

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

发布评论

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

评论(1

筑梦 2024-12-09 08:34:54

您忘记使用表单。用 Html.BeginForm 包围您的字段集,

<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>
  </fieldset>
}
</div>

这将初始化 FormContext 并且您的输入将接收 data-val-* 验证属性

You forgot to use form. Surround your fieldset with Html.BeginForm

<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>
  </fieldset>
}
</div>

This will initialize FormContext and your inputs will receive data-val-* validation attributes

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