在这个 ASP.NET MVC 3 应用程序中,我缺少什么来获得不显眼的客户端验证?
我需要帮助弄清楚如何成功地在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Bugs
和Verbose
是此代码中的公共字段,而不是属性。让它们成为属性,这样就可以解决这个问题。Bugs
andVerbose
are public fields in this code, not properties. Make them properties and that should fix it.为
Bugs
和Verbose
属性定义get;
和set;
。这不仅会纠正(添加)验证,而且将来模型绑定器将能够从服务器上的表单字段绑定回模型。以及关于
Verbose
所需的属性。这是因为需要对值类型进行隐式验证。字符串可以为空,因此如果您没有显式设置Required
属性,则它不是必需的 - 因此 Bug 会保留为不需要的。但从 C# 的本质来看,bool
不能为null
,因此 mvc 会自动为其添加Required
属性。你可以通过以下方式控制它Define
get;
andset;
for bothBugs
andVerbose
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 setRequired
attribute on it explicitely - so Bugs stays without required. Butbool
cannot benull
by nature of c#, so mvc automatically addsRequired
attribute to it. You can control that with我最近遇到了同样的问题(不引人注目的客户端验证在 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.