为给定元素构建数据验证属性列表 [英] Build list of data validation attributes for a given element
问题描述
使用任何输入扩展帮助器方法,例如模型中的验证属性是自动生成的由Razor引擎(通过 <代码> ClientValidationEnabled /
When using any of the Input Extension Helper Methods, like @Html.TextboxFor
, any Validation Attributes from your model are automatically generated by the Razor engine (via ClientValidationEnabled
/UnobtrusiveJavaScriptEnabled
).
例如,采用以下情况,效果很好
For example, take the following case which works fine
模型:
[Required]
public string QuestionOne { get; set; }
查看:
@Html.TextBoxFor(model => model.QuestionOne)
@Html.ValidationMessageFor(model => model.QuestionOne)
生成的标记:
<input type="text" id="QuestionOne" name="QuestionOne" value=""
data-val="true" data-val-required="The QuestionOne field is required." >
<span class="field-validation-valid" data-valmsg-for="QuestionOne" data-valmsg-replace="true"></span>
在这种情况下,属性 data-val ="true"
& data-val-required =必填QuestionOne字段."
由非侵入式验证拾取,并且表单元素已成功验证.
In this case the attributes data-val="true"
& data-val-required="The QuestionOne field is required."
are picked up by Unobtrusive validation and the form element is successfully validated.
但是,出于可扩展性的原因,我希望能够自己生成< input>
元素,而不是使用 TextBoxFor
.所以我的视图现在看起来像这样:
However, for extensibility reasons, I want to be able to generate the <input>
element myself instead of using TextBoxFor
. So my view would now look like this:
<input type="textbox"
id="@Html.IdFor(m => m.QuestionTwo)"
name="@Html.NameFor(m => m.QuestionTwo)"
value="@Model.QuestionTwo"
data-val="true" data-val-required="Selection is Required" />
@Html.ValidationMessageFor(model => model.QuestionTwo)
在这种情况下,我只是通过手工重写 data-val ="true"(等)
来伪造验证属性输出,但这必须扩展为涵盖每一个单例.
In this case, I'm faking the validation attribute output by just re-writing data-val="true" (etc)
by hand, but this would have to be expanded to cover every single case.
问:我可以为给定元素建立/返回一个 data-val-*
属性的列表吗?
Q: Can I build /return a list of data-val-*
attributes for a given element?
推荐答案
您可以使用 HtmlHelper
的 GetUnobtrusiveValidationAttributes()
方法来获取与特定对象关联的验证属性.属性.
You can use the GetUnobtrusiveValidationAttributes()
method of HtmlHelper
to get the validation attributes associated with a specific property.
例如在视图中
@{ var attributes = Html.GetUnobtrusiveValidationAttributes("QuestionTwo"); }
<input
type="textbox"
@foreach(var attr in attributes)
{
@:@attr.Key="@attr.Value"
}
id="@Html.IdFor(m => m.QuestionTwo)"
....
/>
请注意 @:@ attr.Key ="@ attr.Value"
行将给出警告(缺少属性名称),但将正确运行
Note the @:@attr.Key="@attr.Value"
line will give a warning (Missing attribute name) but will run correctly
或者,您可以使用javaScript/jQuery添加属性
Alternatively, you could use javaScript/jQuery to add the attributes
<script type="text/javascript">
var attributes = @Html.Raw(Json.Encode(attributes));
var input = $('#QuestionTwo');
for(var i in attributes) {
input.attr(i, attributes[i]);
}
</script>
我已经分叉了此处的DotNetFiddle ,以显示这两个选项的工作代码.
I have forked the DotNetFiddle here to show the working code for both options.
尽管上面的代码显示了如何完成此操作,但您不应该这样做. HtmlHelper
方法执行许多您忽略的代码,以确保正确的双向模型绑定,例如, value
属性是通过首先检查中的值来确定的> ModelState
,然后在 ViewDataDictionary
中,并且仅当先前值不存在时,它才使用属性的值(TextBox用于显示初始值,而不是从代码中更新的值解释行为).
While the above code shows how it can be done, you should not be doing that. The HtmlHelper
methods execute a lot of code your ignoring to ensure correct 2-way model binding, for example, the value
attribute is determined by first checking for a value in ModelState
, then in the ViewDataDictionary
, and only if the previous values do not exist, does it use the value of the property (the second part of TextBoxFor displaying initial value, not the value updated from code explains the behavior).
除了错误的 value
属性外,您为< input>
显示的代码与仅使用 @Html生成的代码相同.TextBoxFor(m => m.Question2)
.我认为您的实际情况有所不同,但是如果您不能使用 TextBoxFor()
并使用接受 htmlAttributes
的重载来生成所需的html,则采用正确的方法是创建您自己的 HtmlHelper
方法(并利用 HtmlHelper
类和 System.Web.Mvc.Html
命名空间中的现有方法)
Except for the incorrect value
attribute, the code you have shown for the <input>
is the same as will be generated by simply using @Html.TextBoxFor(m => m.Question2)
. I assume your real case is different, but if you cannot make use of TextBoxFor()
and using an overload that accepts htmlAttributes
to generate the html you need, then the correct approach is to create your own HtmlHelper
method (and making use of existing methods in the HtmlHelper
class and System.Web.Mvc.Html
namespace)
这篇关于为给定元素构建数据验证属性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!