验证属性被触发两次

编程入门 行业动态 更新时间:2024-10-15 16:24:17
本文介绍了验证属性被触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的MVC3应用程序中,我有模型(未删除不重要的属性):

In my MVC3 application I have the model ( not important properties deleted ):

public class AccountViewModel { [StringLength(65)] public string Property1 { get; set; } [StringLength(65)] public string Property2 { get; set; } }

问题是,当一个操作提交了两次调用的验证属性时,我得到了4个错误的摘要,而不是2个错误:

The problem is when an action is submited validation attribute called twice, and I can get 4 errors in summary, instead of 2:

'Property1' length must be less than 65 characters 'Property1' length must be less than 65 characters 'Property2' length must be less than 65 characters 'Property2' length must be less than 65 characters

我没有在控制器的代码中使用Validate方法.我的自定义属性也出现了问题,但必需"属性却没有发生此问题.另外我还要注意,自定义属性的ctor也称为两次

I dont use Validate method in my controller's code. The problem appears also with my custom attributes, but its not happens with Required attribute. Also I have to note that ctor of the custom attributes also called twice

我的动作

[HttpPost] public ActionResult CreateOrEdit(AccountViewModel model) { if (!ModelState.IsValid) { return View("Edit", model); } try { _accountService.InsertOrUpdate(model); } catch (Exception ee) { ModelState.AddModelError("", ee.Message); return View("Edit", model); } return RedirectToAction("Index"); }

在查看时,我使用以下方式渲染错误:

On View I render my errors using:

@{ var errors = ViewData.ModelState.Errors(); <div class="alert alert-block alert-error @(errors.Count == 0 ? "hide" : "")" > <h4 class="alert-heading"> You got an error!</h4> <ul> @foreach (var error in errors) { <li>@error</li> } </ul> </div> }

然后我再次再次检查ViewData.ModelState已经包含两次错误.

And I double re-check once more that ViewData.ModelState already contains errors twice.

推荐答案

问题在于集成Ninject.如果您使用 Ninject.MVC 软件包(我使用版本3),则在初始化时会注册自己的ModelValidationProvider并删除旧的:

The problem was in integrating Ninject. If you use Ninject.MVC package ( I use version 3 ) it registers his own ModelValidationProvider while initializing and removes the old one:

在 Ninject.Web.Mvc.MvcModule

this.Kernel.Bind<ModelValidatorProvider>().To<NinjectDataAnnotationsModelValidatorProvider>();

在 Ninject.Web.Mvc.NinjectMvcHttpApplicationPlugin 中:

public void Start() { ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().Single()); DependencyResolver.SetResolver(this.CreateDependencyResolver()); RemoveDefaultAttributeFilterProvider(); }

因此,我没有创建我自己的IDependencyResolver实现(Ninject内核包装器),而是遵循了教程 或者 您应该删除Ninject.MVC程序包,并从bin文件夹中删除其二进制文件.

So, rather than creating my own implementation of IDependencyResolver ( Ninject Kernel wrapper ) I followed this tutorial or you should remove Ninject.MVC package and remove its binaries from the bin folder.

更多推荐

验证属性被触发两次

本文发布于:2023-11-27 13:05:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   属性

发布评论

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

>www.elefans.com

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