数据注释模型绑定器的意图

发布于 2024-09-08 17:24:32 字数 319 浏览 6 评论 0原文

文章此处提到了使用此处提供了数据注释模型绑定器。

有谁知道它的意图是什么?要进行 DA 验证,我不需要 MVC 2.0 中的特殊模型绑定器

The article here mentions the use of Data Annotations Model Binder that is available here.

Does anyone know what it's intent is? To do DA validation, I don't need a special Model binder in MVC 2.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2024-09-15 17:24:32

ASP.Net MVC 的第一个版本不支持通过数据注释作为框架的一部分进行验证。链接的 Codeplex 代码的目的是专门允许使用面向属性的验证(需求量很大)作为第二个版本中实现的解决方案的权宜之计。

The first release of ASP.Net MVC didn't support validation via Data Annotations as part of the framework. The intent of the linked codeplex code was to specifically allow usage of attribute oriented validation (which was in high demand) as a stopgap to the solution that was implemented in the second release.

带刺的爱情 2024-09-15 17:24:32
DataType

Specify the datatype of a property
DisplayName

specify the display name for a property.
DisplayFormat

specify the display format for a property like different format for Date proerty.
Required

Specify a property as required.
ReqularExpression

validate the value of a property by specified regular expression pattern.
Range

validate the value of a property with in a specified range of values.
StringLength

specify min and max length for a string property.
MaxLength

specify max length for a string property.
Bind

specify fields to include or exclude when adding parameter or form values to model properties.
ScaffoldColumn

指定从编辑器表单中隐藏的字段。

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Employee.Models
{
[Bind(Exclude = "EmpId")]
public class Employee
{
[ScaffoldColumn(false)]
public int EmpId { get; set; }
[DisplayName("Employee Name")]
[Required(ErrorMessage = "Employee Name is required")]
[StringLength(100,MinimumLength=3)]
public String EmpName { get; set; }
[Required(ErrorMessage = "Employee Address is required")]
[StringLength(300)]
public string Address { get; set; }
[Required(ErrorMessage = "Salary is required")]
[Range(3000, 10000000,ErrorMessage = "Salary must be between 3000 and 10000000")]
public int Salary{ get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
[MaxLength(50)]
[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]
public string Email { get; set; }
}
}
DataType

Specify the datatype of a property
DisplayName

specify the display name for a property.
DisplayFormat

specify the display format for a property like different format for Date proerty.
Required

Specify a property as required.
ReqularExpression

validate the value of a property by specified regular expression pattern.
Range

validate the value of a property with in a specified range of values.
StringLength

specify min and max length for a string property.
MaxLength

specify max length for a string property.
Bind

specify fields to include or exclude when adding parameter or form values to model properties.
ScaffoldColumn

specify fields for hiding from editor forms.

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Employee.Models
{
[Bind(Exclude = "EmpId")]
public class Employee
{
[ScaffoldColumn(false)]
public int EmpId { get; set; }
[DisplayName("Employee Name")]
[Required(ErrorMessage = "Employee Name is required")]
[StringLength(100,MinimumLength=3)]
public String EmpName { get; set; }
[Required(ErrorMessage = "Employee Address is required")]
[StringLength(300)]
public string Address { get; set; }
[Required(ErrorMessage = "Salary is required")]
[Range(3000, 10000000,ErrorMessage = "Salary must be between 3000 and 10000000")]
public int Salary{ get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
[MaxLength(50)]
[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]
public string Email { get; set; }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文