客户端自定义数据注解验证

编程入门 行业动态 更新时间:2024-10-28 17:26:22
本文介绍了客户端自定义数据注解验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经创建自定义数据注解做我的视图模型一些验证。的问题是,它不能在客户端验证。这里是我的模型:

I've create a custom data annotation to do some validation on my view model. The problem is that it doesn't validate on the client-side. Here's my model:

public class MemberViewModel { [ScaffoldColumn(false)] public int MemberId { get; set; } [Required(ErrorMessage = "Name is required")] public string Name { get; set; } //My custom data annotation [EnforceTrue(ErrorMessage = "You must agree to the Terms and Conditions")] public bool AgreeTerms { get; set; } }

我的数据标注验证code:

My data annotation validation code:

public class EnforceTrueAttribute : ValidationAttribute, IClientValidatable { public EnforceTrueAttribute() { } public override bool IsValid(object value) { return value != null && (bool)value == true; } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { yield return new ModelClientValidationRule() { ValidationType = "enforcetrue", ErrorMessage = this.ErrorMessageString }; } }

我的控制器方法:

My controller method:

[HttpPost] public ActionResult Index(MemberViewModel viewModel) { Member member = new Member(); TryUpdateModel(member); if (ModelState.IsValid) { _membersRepository.SaveMember(member); return RedirectToAction("Index", "Home"); } return View(viewModel); // validation error, so redisplay same view }

和我的看法:

@using (Html.BeginForm("Index", "Members", FormMethod.Post)) { @Html.HiddenFor(m => m.MemberId) <div class="editor-label">@Html.LabelFor(model => model.Name)</div> <div class="editor-field">@Html.TextBoxFor(model => model.Name)</div> <div class="editor-field">@Html.CheckBoxFor(model => model.AgreeTerms) <label for="AgreeTerms">I agree to the Terms and Conditions</label></div> <p> <input type="submit" value="Submit" /> </p> @Html.ValidationSummary() }

所以我所有的其他错误消息得到显示在客户端验证的验证摘要。但是,对于我的自定义数据注解,该错误信息不会显示,直到模型的其余部分是有效的,以后你提交表单并重新加载页面时,将显示在摘要中的错误的。

So all my other error messages get displayed in the validation summary with client-side validation. But for my custom data annotation, the error message doesn't show until the rest of the model is valid, and after you submit the form and page reloads, that's when the error is displayed in the summary.

有没有别的我需要在这里做的就是它在总结显示与其他错误的东西吗?

Is there something else I need to do here to get it to show up in the summary with the other errors?

我使用C#和ASP.NET MVC 3

I'm using C# and ASP.NET MVC 3

推荐答案

最近有同样的问题。你可以写:

Had same issue recently. You can write:

$.validator.addMethod('enforcetrue', function (value, element) { return $(element).is(":checked"); }); $.validator.unobtrusive.adapters.add('enforcetrue', [], function (options) { options.messages['enforcetrue'] = options.message; options.rules['enforcetrue'] = options.params; });

这里的类似的问题href=\"stackoverflow/questions/4784943/asp-net-mvc-3-client-side-validation-with-parameters\">ASP.NET MVC 3客户端验证

更多推荐

客户端自定义数据注解验证

本文发布于:2023-08-07 15:05:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319770.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   注解   客户端   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!