使用 Lightspeed 进行 MVC3 验证
我的 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] 属性:
未添加 [Required] 属性:(注意 LightSpeed验证的奇怪渲染)
填写名称:
在上面的图像中 - 顶部的验证是 LightSpeed(放入 ValidationSummary),侧面是 MVC3(放入 ValidationMessageFor)
目前仅使用服务器端验证。
问题:如何让 LightSpeed 验证在 MVC3 中正常运行?
我认为这是这个领域的东西 http ://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part4/
对于服务器端验证 - 您将需要使用发出错误的自定义模型绑定器更准确地来自 LightSpeed 验证,而不是利用 DefaultModelBinder 行为。查看直接使用或改编 Mvc 社区代码库中的 EntityModelBinder
My ORM (LightSpeed) generates this for Animals table, with Name and Age. Using MVC3 and 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"); }
}
With [Required] attribute added:
With no [Required] attribute added: (notice LightSpeed strange rendering of validation)
With name filled in:
In images above - the validation at the top is LightSpeed (put into ValidationSummary) and at the side is MVC3 (put into ValidationMessageFor)
Am only using Server Side validation currently.
Question: How do I get LightSpeed validation working well in MVC3?
I think it is something in this area http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part4/
For the server side validation - you will want to use a custom model binder which emits the errors from LightSpeed validation more precisely rather than the leveraging the DefaultModelBinder behavior. Have a look at either directly using or adapting the EntityModelBinder from the community code library for Mvc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅链接 http://www.mindscapehq.com/forums/Thread.aspx?ThreadID =4093
Jeremys的回答(Mindscape有很大的支持!)
并在Global.asax中注册使用:
EntityModelBinder2.Register(typeof(MyEntity).Assembly);
Register 调用设置要用于模型装配中每个实体类型的模型绑定器,因此请根据需要进行修改。
See link http://www.mindscapehq.com/forums/Thread.aspx?ThreadID=4093
Jeremys answer (Mindscape have great support!)
and in Global.asax register using:
EntityModelBinder2.Register(typeof(MyEntity).Assembly);
The Register call sets up the model binder to be used for each entity type in your model assembly so modify as required.
从 2011 年 4 月 4 日起,您可以使用 Lightspeed 夜间版本进行客户端验证。
创建验证器提供程序如下:
然后在 Application_Start() 中添加
You can get client side validation working with Lightspeed nightly builds from 04/04/2011 onwards.
Create a validator provider as follows:
Then in Application_Start() add