我的ORM(LightSpeed)为动态表生成此名称,其中包含姓名和年龄。使用MVC3和Razor
[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"); }
}
添加[必需]属性:
未添加[Required]属性:(注意LightSpeed奇怪的渲染验证)
填写姓名:
在上面的图片中 - 顶部的验证是LightSpeed(放入ValidationSummary),旁边是MVC3(放入ValidationMessageFor)
目前仅使用服务器端验证。
问题:如何让LightSpeed验证在MVC3中运行良好?
我认为这个领域是http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part4/
对于服务器端验证 - 您将需要使用自定义模型绑定器,它可以更精确地发出LightSpeed验证中的错误,而不是利用DefaultModelBinder行为。看一下直接使用或调整社区代码库中的EntityModelBinder以获取Mvc
答案 0 :(得分:1)
请参阅链接http://www.mindscapehq.com/forums/Thread.aspx?ThreadID=4093
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运算(myEntity所).Assembly);
Register调用设置模型装订器,用于模型装配中的每个实体类型,以便根据需要进行修改。
答案 1 :(得分:0)
从04/04/2011开始,您可以使用Lightspeed nightly版本进行客户端验证。
按如下方式创建验证器提供程序:
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());