asp.net-mvc – 自定义模型绑定器不验证模型
发布时间:2020-12-15 19:17:17 所属栏目:asp.Net 来源:网络整理
导读:我开始玩knockout.js,在这样做的时候我使用了FromJsonAttribute(由Steve Sanderson创建).我遇到一个问题,自定义属性不执行模型验证.我把一个简单的例子放在一起 – 我知道它看起来像很多代码 – 但是基本的问题是如何强制在自定义模型绑定器中验证模型. usin
我开始玩knockout.js,在这样做的时候我使用了FromJsonAttribute(由Steve Sanderson创建).我遇到一个问题,自定义属性不执行模型验证.我把一个简单的例子放在一起 – 我知道它看起来像很多代码 – 但是基本的问题是如何强制在自定义模型绑定器中验证模型.
using System.ComponentModel.DataAnnotations; namespace BindingExamples.Models { public class Widget { [Required] public string Name { get; set; } } } 这里是我的控制器: using System; using System.Web.Mvc; using BindingExamples.Models; namespace BindingExamples.Controllers { public class WidgetController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(Widget w) { if(this.ModelState.IsValid) { TempData["message"] = String.Format("Thanks for inserting {0}",w.Name); return RedirectToAction("Confirmation"); } return View(w); } [HttpPost] public ActionResult PostJson([koListEditor.FromJson] Widget w) { //the ModelState.IsValid even though the widget has an empty Name if (this.ModelState.IsValid) { TempData["message"] = String.Format("Thanks for inserting {0}",w.Name); return RedirectToAction("Confirmation"); } return View(w); } public ActionResult Confirmation() { return View(); } } } 我的问题是模型总是在我的PostJson方法中有效.为了完整性,这里是FromJson属性的Sanderson代码: using System.Web.Mvc; using System.Web.Script.Serialization; namespace koListEditor { public class FromJsonAttribute : CustomModelBinderAttribute { private readonly static JavaScriptSerializer serializer = new JavaScriptSerializer(); public override IModelBinder GetBinder() { return new JsonModelBinder(); } private class JsonModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) { var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName]; if (string.IsNullOrEmpty(stringified)) return null; var model = serializer.Deserialize(stringified,bindingContext.ModelType); return model; } } } } 解决方法
描述
FromJsonAttribute只绑定到模型,如你所说,没有验证. 您可以向FromJsonAttribute添加验证,以便根据其DataAnnotations属性验证模型. 这可以使用TypeDescriptor类完成.
查看我的解决方案我已经测试了 解 private class JsonModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) { var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName]; if (string.IsNullOrEmpty(stringified)) return null; var model = serializer.Deserialize(stringified,bindingContext.ModelType); // DataAnnotation Validation var validationResult = from prop in TypeDescriptor.GetProperties(model).Cast<PropertyDescriptor>() from attribute in prop.Attributes.OfType<ValidationAttribute>() where !attribute.IsValid(prop.GetValue(model)) select new { Propertie = prop.Name,ErrorMessage = attribute.FormatErrorMessage(string.Empty) }; // Add the ValidationResult's to the ModelState foreach (var validationResultItem in validationResult) bindingContext.ModelState.AddModelError(validationResultItem.Propertie,validationResultItem.ErrorMessage); return model; } } 更多信息 > TypeDescriptor Class (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 您实际可以拥有多少应用程序会话数据?
- asp.net – NHibernate线程安全与会话
- asp.net-web-api – 在ASP.NET Web API控制器的nunit测试中
- asp.net-mvc – 在ASP.NET Identity中手动验证密码重置令牌
- asp.net – 如何拒绝访问文件夹或文件
- asp.net-mvc – ELMAH和SQL Server 2008 R2?
- asp.net – WebBrowsable vs个性化Web部件
- asp.net-mvc – 用相同的动词重载asp.net MVC控制器方法?
- 跨域学习笔记1--跨域调用webapi
- asp.net-mvc – 无法使用IIS Express在Windows 10上运行Web
推荐文章
站长推荐
- 如何从asp.net HyperLink控件调用javascript
- 在ASP.NET MVC 5中创建对象时何时在C#中使用NEW关
- 谈谈分布式事务之三: System.Transactions事务详
- asp.net-mvc – 用于MVC2的ASP.Net MVC期货更新
- asp.net – 内部图像和文本的asp:LinkBut??ton在
- asp.net-mvc – 检测到潜在的危险Request.Form值
- 一键式ASP.NET Web部署包括先决条件?
- asp.net-mvc – 带负载均衡器的SignalR v2
- asp.net-mvc-4 – URL.Action参数中的客户端值
- 您可以为标准ASP.NET Web窗体验证器定制客户端Ja
热点阅读