Tag: data annotations

asp.net mvc int属性绑定exception

我的类中有一个int属性,并想validation用户是否输入了一个字符串。 如何使用数据注释来做到这一点? 当我传递一个非整数值时,我得到一个像这样的excpetion: The value ‘asdasd’ is not valid for property. 例如,使用此validation属性: [Range(0, Int32.MaxValue, ErrorMessage=”Invalid Number”)] public int? Number { get; set; } 并在使用该模型的字段中输入’aaa’我已经对此消息进行了检查: The value ‘aaa’ is not valid for Number. 而不是“无效数字”消息。 有任何想法吗? 我已经把 [Range(0, Int32.MaxValue, ErrorMessage=”Invalid Number”)] public int? Number { get; set; } 但是我从这个消息中得到了这个消息 The value ‘aaa’ is not valid for Number.

数据注释未显示MVC 4中的部分实体类

我已经看到了几十个关于如何通过部分类将元数据注释添加到通过entity framework生成的类的解释, 数据库优先 。 有人能告诉我为什么这些新的显示值没有显示在我的视图中吗? 这两个都是与我的entity framework生成的类相同的命名空间的一部分。 [MetadataType(typeof(xRef_CodesMetadata))] public partial class xRef_Codes { } public class xRef_CodesMetadata { public int CodeID { get; set; } public int CTB_ID { get; set; } [Required(ErrorMessage = “Please type a name”)] [Display(Name = “Code Name”)] [Column(TypeName = “Code Name”)] public string CodeName { get; set; } [Required(ErrorMessage = […]

C#将DataAnnotations添加到EntityFramework中的实体

我正在使用ADO.Netentity framework。 为了处理输入validation,我正在尝试使用DataAnnotations ,我查看了StavkOverflow和Google,并且我发现几乎所有使用MetadataType示例。 但是,我已经尝试了几个小时而且我无法使它工作..出于某种原因, EmployeeMetaData类中的CustomAttributes没有应用于EmployeeMetaData类上的相应字段/属性。 有谁知道为什么会发生这种情况? 是的,我确信属性类型和名称完全匹配。 感谢任何帮助,我已经坚持了几个小时。 提前致谢。 EntityExtentions.cs [MetadataType(typeof(EmployeeMetaData))] public partial class Employee:IDataErrorInfo { public string Error { get { return String.Empty; } } public string this[string property] { get { return EntityHelper.ValidateProperty(this, property); } } } public class EmployeeMetaData { [Required(AllowEmptyStrings=false, ErrorMessage = “A name must be defined for the employee.”)] […]

自定义ValidationAttribute不起作用。 始终返回true

我创建了一个自定义的ValidationAttribute类来检查我的应用程序中的人的年龄: public class MinimumAgeAttribute : ValidationAttribute { public int MinAge { get; set; } public override bool IsValid(object value) { return CalculateAge((DateTime) value) >= MinAge; } private int CalculateAge(DateTime dateofBirth) { DateTime today = DateTime.Now; int age = today.Year – dateofBirth.Year; if (dateofBirth > today.AddYears(-age)) age–; return age; } } 数据注释在字段上设置如下: [MinimumAge(MinAge = 18, ErrorMessage […]

entity framework6重用数据注释

我已经找了一段时间才能找到明确的解决方案,但还没有得出结论。 我想在数据模型类上只指定一次数据注释,并且UI可以从视图模型类中看到它们,而无需再次指定它们。 为了说明我的观点,假设我有一个UserAccount类…… public class UserAccount { [Display(Name = “Username”, Prompt = “Login As”), Required()] string UserName { get; set; } [Display(Name = “Password”, Prompt = “Password”), Required(), DataType(DataType.Password), StringLength(255, MinimumLength = 7, ErrorMessage = “The password must be at least 7 characters”)] string Password { get; set; } } 现在我想指定一个视图模型,其中包含一个密码确认字段,该字段不会存储在数据库中,也可能存储在其他数据模型中,但我不想再次指定所有数据注释属性。 这是关于尝试找到最佳实践,但也需要维护多个声明,在某些时候,一个将被修改,而其他声明将不会。 我已经看过一个接口/inheritance解决方案,但由于各种原因,它并没有完全削减它。 另一个可能的解决方案可能是在View Model类(或属性)上有一个Attribute来表示从…inheritance属性但是我找不到任何合适的东西。 […]

有没有办法重用数据注释?

有没有办法在ASP.Net MVC 4中的视图中用作模型的类内部实现数据域(在属性级别)的概念? 考虑以下代码: public class LoginProfileModel { [DisplayName(“Login ID”)] [Required(ErrorMessage = “Login ID is required.”)] public string LogonID { get; set; } [DisplayName(“Password”)] [Required(ErrorMessage = “Password cannot be blank.”)] [StringLength(20, MinimumLength = 3)] [DataType(DataType.Password)] public string Password { get; set; } } 这是ASP.Net MVC 4中的LoginProfileModel。它使用各种元数据/数据注释,以便我可以使用以下代码创建一个干净的视图: @model myWebSite.Areas.People.Models.LoginProfileModel @using ( Html.BeginForm( “Index” , “Login” ) […]

WebApi POST不包含ID字段

我仍然只需要几天的ASP.NET和WebAPI框架,所以我必须错过一些非常简单的东西。 我有一个模型,有一些属性和ID(作为一个属性,有一个私有的setter但没有帮助)。 public long ID { get; private set; } [Required(ErrorMessage = “Location coordinate X is required.”)] public double X { get; set; } [Required(ErrorMessage = “Location coordinate Y is required.”)] public double Y { get; set; } 然后我有一个控制器方法post: public HttpResponseMessage Post(MyModel model) 当我启动项目并转到自动生成的API文档时,我可以看到样本包含ID作为输入字段。 我希望API忽略ID输入字段。 我可以自己忽略它,但我不喜欢我的代码中那些必须记住 – 不要使用的东西。 一种选择是为输入创建一个单独的模型,但这意味着我必须维护两个类而不是一个。 是否有任何数据注释完全忽略此属性?

如何根据DataAnnotation中的另一个属性validation属性

考虑我有这两个属性: public class Test { [Required(ErrorMessage = “Please Enetr Age”)] public System.Int32 Age { get; set; } [Required(ErrorMessage = “Choose an option”)] public System.Boolean IsOld { get; set; } } 当用户为Age输入例如15并为IsOld选择“ Yes ”时,我返回一个正确的Age或IsOld的exception。 我已经使用了CustomValidation ,但因为我的validation必须是静态的,所以我无法访问其他属性。 我如何通过DataAnnotation执行此操作?

添加数据注释到Entity Framework(或Linq to SQL)生成的类

是否可以自动将更多Data Anootation成员(如Range , Required ,…)添加到Entity Framework或Linq to SQL生成的类? 我想对我的类使用数据注释validation 谢谢 要点:与此主题相关: 使用元数据和entity framework来validation使用数据注释 编辑1) 我为Northwind数据库创建一个entity framework模型并添加Product类。代码的一部分是这样的: [EdmEntityTypeAttribute(NamespaceName=”NorthwindModel”, Name=”Product”)] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class Product : EntityObject { #region Factory Method /// /// Create a new Product object. /// /// Initial value of the ProductID property. /// Initial value of the ProductName property. /// Initial value […]

ASP.NET MVC4:属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

我有以下代码: [Required(ErrorMessage = MessageModel.translateMessage(“required”)))] [Display(Name= MessageModel.translateMessage(“id”))] public string user_id { get; set; } 我试图使错误消息动态,但编译时出错: “An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type.” 解决这个问题的任何方法?