asp.net-mvc-3 – 向@ Html.ValidationSummary添加错误消息
发布时间:2020-12-15 18:44:03 所属栏目:asp.Net 来源:网络整理
导读:我使用标准的MVC3 Razor视图与不引人注目的Javascript验证,使用@ Html.ValidationSummary来显示它们在表单的顶部。如果标准验证(例如[必需])通过,那么我将在用户点击Submit按钮时运行一些非常自定义的客户端验证。 (验证会查看许多表单元素,以确保其正确
我使用标准的MVC3 Razor视图与不引人注目的Javascript验证,使用@ Html.ValidationSummary来显示它们在表单的顶部。如果标准验证(例如[必需])通过,那么我将在用户点击Submit按钮时运行一些非常自定义的客户端验证。 (验证会查看许多表单元素,以确保其正确的一组已经被检查等等,因此它不像为单个字段创建新的自定义验证器那么简单)。
我想要构建那里的可能的错误显示在ValidationSummary列表中,但是我不知道如何让错误信息出现在那里。 解决方法
在客户端:
function YourCustomValidator() { // do your validation logic here via JavaScript return true; // or false based on your validation logic } $(document).ready(function () { // take your own form-selector like ("form",this) $("form",this).first().submit(function () { return (YourCustomValidator() && $(this).valid()); }); }); 或在服务器端: 认为你有这样的模型: public class Test { [Required] [StringLength(100)] public string FullName { get; set; } } 当您验证它时: if(ModelState.IsValid) { // default validations run here if(/* some custom validations run here,there is an error about "FullName" */){ // you should set the "key" for Model-Error to "FullName" ModelState.AddModelError("FullName","error-message goes here") } if(/* some custom validations run here,the error is global,not on "FullName" */){ // you should set the "key" for Model-Error to an empty-string ModelState.AddModelError("","error-message goes here") } // also you can test for model-errors again like this: if(ModelState.IsValid) { // if you add any error above,this will be "false" } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如果在Razor MVC 3中浏览器未继承视图时如何
- 您可以为标准ASP.NET Web窗体验证器定制客户端JavaScript验
- asp.net – 名称空间“CrystalDecisions.Web”中不存在类型
- asp.net – 在ASCX自定义控件中放置CSS规则的位置?
- asp.net – 程序或函数期望未提供的参数
- asp.net-mvc-3 – VSTS错误SerializationException
- asp.net – 表单身份验证无法正确验证用户
- asp.net-mvc – 为现有项目添加标识
- asp.net-web-api2 – 通过http Post请求将XML发送到Web Api
- Pro ASP.NET Core MVC 2翻译计划:第一章
推荐文章
站长推荐
- asp.net-mvc-3 – ASP.NET MVC 3从控制器返回.cs
- 在ASP.NET中使用新的会话ID打开一个新的浏览器窗
- asp-classic – 如何在asp经典中触发异步调用并忽
- asp.net – VirtualPathUtility.ToAbsolute()VS.
- asp.net-mvc – 使用Ajax.Beginform的RedirectTo
- asp.net – 如何使用Excel服务器端?
- 如何在我的ASP.NET应用程序中创建“通用错误”页
- asp.net – System.Data.OleDb.OleDbException:
- 是否可以在IIS 7中将虚拟目录设置为根目录?
- asp.net-mvc – 已经处理了DbContext和autofac
热点阅读