2011-03-20 46 views
1

我的ORM(LightSpeed)为动物表生成此名称,其中包含名称和年龄。使用MVC3和剃刀使用Lightspeed MVC3验证

[Serializable] 
    [System.CodeDom.Compiler.GeneratedCode("LightSpeedModelGenerator", "1.0.0.0")] 
    [System.ComponentModel.DataObject] 
    [Table(IdColumnName="AnimalID", IdentityMethod=IdentityMethod.IdentityColumn)] 
    public partial class Animal : Entity<int> 
    {  
    [ValidatePresence] 
    [ValidateLength(0, 50)] 
    private string _name; 

    [ValidateComparison(ComparisonOperator.GreaterThan, 0)] 
    private int _age; 

    public const string NameField = "Name"; 
    public const string AgeField = "Age"; 

    [System.Diagnostics.DebuggerNonUserCode] 
    [Required] // ****I put this in manually to get Name required working 
    public string Name 
    { 
     get { return Get(ref _name, "Name"); } 
     set { Set(ref _name, value, "Name"); } 
    } 

    [System.Diagnostics.DebuggerNonUserCode] 
    public int Age 
    { 
     get { return Get(ref _age, "Age"); } 
     set { Set(ref _age, value, "Age"); } 
    } 

随着[必需]属性补充说:

enter image description here

由于没有[必需]属性补充:(注意光速奇怪验证渲染)

enter image description here

名称填入:

enter image description here

在上面的图像 - 顶部的验证是光速(投入的ValidationSummary),并在侧是MVC3(投入ValidationMessageFor)仅

正在使用服务器端验证当前。

问题:我如何在MVC3中正常使用LightSpeed验证?

我认为这是一些在这个领域http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part4/

对于服务器端验证 - 您将要使用从光速验证更准确,而不是借力DefaultModelBinder行为发出错误的自定义模型粘合剂。看看直接使用或从社区代码库适应EntityModelBinder MVC的

http://www.mindscapehq.com/forums/Thread.aspx?PostID=12051

回答

1

见链接http://www.mindscapehq.com/forums/Thread.aspx?ThreadID=4093

Jeremys答案(思维空间有很大的支持!)

public class EntityModelBinder2 : DefaultModelBinder 
    { 
    public static void Register(Assembly assembly) 
    { 
     ModelBinders.Binders.Add(typeof(Entity), new EntityModelBinder2()); 

     foreach (Type type in assembly.GetTypes()) 
     { 
     if (typeof(Entity).IsAssignableFrom(type)) 
     { 
      ModelBinders.Binders.Add(type, new EntityModelBinder2()); 
     } 
     } 
    } 

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     object result = base.BindModel(controllerContext, bindingContext); 

     if (typeof(Entity).IsAssignableFrom(bindingContext.ModelType)) 
     { 
     Entity entity = (Entity)result; 

     if (!entity.IsValid) 
     { 
      foreach (var state in bindingContext.ModelState.Where(s => s.Value.Errors.Count > 0)) 
      { 
      state.Value.Errors.Clear(); 
      } 

      foreach (var error in entity.Errors) 
      { 
      if (error.ErrorMessage.EndsWith("is invalid")) continue; 
      bindingContext.ModelState.AddModelError(error.PropertyName ?? "Custom", error.ErrorMessage); 
      } 
     } 
     } 

     return result; 
    } 
    } 

并在Global.asax寄存器中使用:

EntityModelBinder2.Register(typeof(MyE ntity).Assembly);

注册调用将设置模型联编程序以用于模型程序集中的每个实体类型,以便根据需要进行修改。

0

从04/04/2011开始,您可以使用Lightspeed夜间版本进行客户端验证。

创建验证提供商如下:

public class LightspeedModelValidatorProvider : DataAnnotationsModelValidatorProvider 
{ 
    private string GetDisplayName(string name) 
    { 
     return name; // go whatever processing is required, eg decamelise, replace "_" with " " etc 
    } 

    protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes) 
    { 

     if(typeof(Entity).IsAssignableFrom(metadata.ContainerType)) 
     { 
      List<Attribute> newAttributes = new List<Attribute>(attributes); 
      var attr = DataAnnotationBuilder.GetDataAnnotations(metadata.ContainerType, metadata.PropertyName, GetDisplayName(metadata.PropertyName)); 
      newAttributes.AddRange(attr); 

      return base.GetValidators(metadata, context, newAttributes); 
     } 

     return base.GetValidators(metadata, context, attributes); 
    } 
} 
中的Application_Start

然后()添加

 ModelValidatorProviders.Providers.Clear(); 
     ModelValidatorProviders.Providers.Add(new LightspeedModelValidatorProvider());