我正在使用 ASP.NET MVC、MySQL 和 NHibernate 开发一个小站点。
我有一个联系人类:
[ModelBinder(typeof(CondicaoBinder))]
public class Contact {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int Age { get; set; }
}
和模型粘合剂:
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 中?还是其他地方?
非常感谢。