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) {
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • 浅析.NET下XML数据访问新机制
  • asp.net 面试+笔试题目第1/2页
  • C# 邮件地址是否合法的验证
  • asp.net 设置GridView的选中行的实现代码
  • C#高级编程:数据库连接[1]
  • 经典C++程序1
  • IIS 自动回收导致后台定时器失效的问题解决
  • ASP.NET&#160;GridView列表代码示例
  • Asp.net MVC源码分析--Action Filter的链式调用
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1