Asp.net MVC源码分析--Model Validation(Client端)实现(1)(3)
来源:未知 责任编辑:责任编辑 发表时间:2013-11-26 22:14 点击:次
6 @Html.ValidationMessageFor(m => m.UserName)
7 </div>
复制代码
也就是说Client Validaton 信息是通过Html.TextBoxFor方法来输出的,那么我们接下来研究一下这个方法是如何实现的。
在InputExtension.cs 方件的InputHelper方法我们找到了一些与Client Validaton有关的代码片段,下面第9行代码的GetUnobtrusiveValidationAttributes方法就实现了输出。
1 // If there are any errors for a named field, we add the css attribute.
2 ModelState modelState;
3 if (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState)) {
4 if (modelState.Errors.Count > 0) {
5 tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
6 }
7 }
8
9 tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));
复制代码
GetUnobtrusiveValidationAttributes方法的源码:
1 // Only render attributes if unobtrusive client-side validation is enabled, and then only if we've
2 // never rendered validation for a field with this name in this form. Also, if there's no form context,
3 // then we can't render the attributes (we'd have no <form> to attach them to).
4 public IDictionary<string, object> GetUnobtrusiveValidationAttributes(string name, ModelMetadata metadata) {
5 Dictionary<string, object> results = new Dictionary<string, object>();
6
7 // The ordering of these 3 checks (and the early exits) is for performance reasons.
8 if (!ViewContext.UnobtrusiveJavaScriptEnabled) {
9 return results;
10 }
11
12 FormContext formContext = ViewContext.GetFormContextForClientValidation();
13 if (formContext == null) {
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>