如何[以及在何处]使用 ModelBinder 实现验证
我正在使用 ASP.NET MVC、MySQL 和 NHibernate 开发一个小网站。
我有一个 Contact 类:
[ModelBinder(typeof(CondicaoBinder))]
public class Contact {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int Age { get; set; }
}
和一个 Model Binder:
public class ContactBinder:IModelBinder {
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
Contact contact = new Contact ();
HttpRequestBase form = controllerContext.HttpContext.Request;
contact.Id = Int16.Parse(form["Id"]);
contact.Name = form["Name"];
contact.Age = Int16.Parse(form["Age"]);
return contact;
}
}
另外,我有一个带有表单的视图,可以使用以下操作更新我的数据库:
public ActionResult Edit([ModelBinder(typeof(ContactBinder))] Contact contact) {
contactRepo.Update(contact);
return RedirectToAction("Index", "Contacts");
}
直到这里,一切都工作正常。但在更新我的联系人之前,我必须实施表单验证。
我的问题是:我应该在哪里实施此验证?在 ActionResult 方法中还是在 Model Binder 中?或者其他地方?
非常感谢。
I'm developing a little site using ASP.NET MVC, MySQL and NHibernate.
I have a Contact class:
[ModelBinder(typeof(CondicaoBinder))]
public class Contact {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int Age { get; set; }
}
And a Model Binder:
public class ContactBinder:IModelBinder {
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
Contact contact = new Contact ();
HttpRequestBase form = controllerContext.HttpContext.Request;
contact.Id = Int16.Parse(form["Id"]);
contact.Name = form["Name"];
contact.Age = Int16.Parse(form["Age"]);
return contact;
}
}
Also, I have a view with a form to update my database, using this action:
public ActionResult Edit([ModelBinder(typeof(ContactBinder))] Contact contact) {
contactRepo.Update(contact);
return RedirectToAction("Index", "Contacts");
}
Until here, everything is working fine. But I have to implement a form validation, before update my contact.
My question is: Where should I implement this validation? In ActionResult method or in Model Binder? Or anywhere else?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看 Steve Sanderson 的 XVAL。
您的业务对象是您的业务逻辑应该应用的地方。
仁慈
丹
XVal
Have a look at XVAL by Steve Sanderson.
Your business objects are where your business logic should be applied.
Kindness
Dan
XVal
我支持史蒂夫·桑德森,他的书很棒。
我真的很喜欢 Rob Conery、Scott Hanselman、Phil Haack、Scott Guthrie 写的书呆子晚餐方法。基本上,每个实体都有一个根据业务逻辑进行验证的方法。该方法返回包含字段/错误消息的 RuleViolations 列表。为了方便起见,您还公开了一个 bool 值。
您可以在此处获取免费章节:书呆子晚餐章节
I second Steve Sanderson, his book is amazing.
I've really liked the nerd dinner approach written by Rob Conery, Scott Hanselman, Phil Haack, Scott Guthrie. Basically you have a method in each entity that validates against buisness logic. That method returns a list of RuleViolations that contain the field / error msg. You also expose a bool value for convience.
You get the free chapter here: Nerd Dinner Chapter
我认为这种情况最好遵循微软的建议,即 使用服务层进行验证
I think this case it is better follow Microsoft recommendation that is Validation with Service Layer