Tag: validationattribute

从模型中获取数据注释属性

我想创建自定义客户端validation器,但我想通过业务逻辑层的Data Annotations属性定义validation规则。 如何在运行时访问模型validation属性? 我想写’generator’,它会转换这段代码: public class LoginModel { [Required] [MinLength(3)] public string UserName { get; set; } [Required] public string Password { get; set; } } 进入这一个: var loginViewModel= { UserName: ko.observable().extend({ minLength: 3, required: true }), Password: ko.observable().extend({ required: true }) }; 但当然不是来自.cs来源。 =) 也许反思? UPD 我发现了这个方法: MSDN 。 但无法理解如何使用它。

自定义validation属性,用于将my属性的值与模型类中的另一个属性值进行比较

我想创建一个自定义validation属性,我想在其中将my属性的值与我的模型类中的另一个属性值进行比较。 例如我在我的模型类中: … public string SourceCity { get; set; } public string DestinationCity { get; set; } 我想创建一个自定义属性来像这样使用它: [Custom(“SourceCity”, ErrorMessage = “the source and destination should not be equal”)] public string DestinationCity { get; set; } //this wil lcompare SourceCity with DestinationCity 我要怎么去那儿?

电话号码validationMVC

我正在尝试使用正则表达式来validation电话号码,并在提交无效的号码或电话号码时返回错误。 MVC代码 : Phone Number: @Html.TextBoxFor(model => model.PhoneNumber, new { @class = “textbox” }) @Html.ValidationMessageFor(model => model.PhoneNumber) C#代码 : [DataType(DataType.PhoneNumber)] [Display(Name = “Phone Number”)] [Required(ErrorMessage = “Phone Number Required!”)] [RegularExpression(@”^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$”, ErrorMessage = “Entered phone format is not valid.”)] public string PhoneNumber { get; set; } 但是,输入框不会向用户显示消息,表明提交的电话号码无效。