data-annotations

DataAnnotations and Resources don't play nicely

﹥>﹥吖頭↗ 提交于 2019-12-29 06:49:13
问题 I'm using dataannotations in an MVC2 app and am a little discouraged when trying to use RESX file resources for error messages. I've tried the following but keep getting the exception "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type" [Required(ErrorMessage = Resources.ErrorMessages.Required)] [Required(ErrorMessageResourceName = Resources.ErrorMessages.Required, ErrorMessageResourceType = typeof(Resources

Server-side validation of a REQUIRED String Property in MVC2 Entity Framework 4 does not work

て烟熏妆下的殇ゞ 提交于 2019-12-29 04:03:06
问题 I'm trying to get server-side validation of an Entity Framework String Property to work. Other server-side validation such as data type validation and required dateTime and numeric EF properties are working. This in VS 2010, .Net 4.0, MVC2 + Cloud, ADO.Net Entity Framework. The String Property I am having issues with is mapped to a SQL 2008, Varchar(50) non-nullable column. When I try to post to my Create action with an empty string for this Property, I get the follwing error. Exception

Server-side validation of a REQUIRED String Property in MVC2 Entity Framework 4 does not work

痴心易碎 提交于 2019-12-29 04:03:03
问题 I'm trying to get server-side validation of an Entity Framework String Property to work. Other server-side validation such as data type validation and required dateTime and numeric EF properties are working. This in VS 2010, .Net 4.0, MVC2 + Cloud, ADO.Net Entity Framework. The String Property I am having issues with is mapped to a SQL 2008, Varchar(50) non-nullable column. When I try to post to my Create action with an empty string for this Property, I get the follwing error. Exception

How to override default required error message

别来无恙 提交于 2019-12-29 02:04:12
问题 I have an old C# MVC 2.0 web application. Whenever I use a [Required] attribute, the default validation error message goes: The [whatever] field is required. My problem is that the application isn't in English, so I basically have to change the attribute call to [Required(ErrorMessage = "Le champ [whatever] est requis.")] everywhere. Is there a way to override the default error message so I only have to specify it when I want a specific message? I'm looking for something like:

Why can't I use resources as ErrorMessage with DataAnnotations?

夙愿已清 提交于 2019-12-28 05:38:05
问题 Why can't I do like this? [Required(ErrorMessage = "*")] [RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessage = Resources.RegistrationModel.UsernameError)] public string Username { get; set; } What is the error message telling me? An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type. 回答1: When you are using the ErrorMessage property only constant strings or string literal can be assigned to it. Use the

Create database index with Entity Framework

淺唱寂寞╮ 提交于 2019-12-28 05:30:27
问题 Say I have the following model: [Table("Record")] public class RecordModel { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] [Display(Name = "Record Id")] public int RecordId { get; set; } [StringLength(150)] public string Name { get; set; } [Required] [StringLength(15)] public string IMEI { get; set; } } Is it possible to add an index to the IMEI column through using an attribute, data annotation, or something from the model? 回答1: According to this link: Creating Indexes

ASP.NET Core MetaDataType Attribute not working

早过忘川 提交于 2019-12-28 01:25:50
问题 I'm using the MetaDataType Attribute on my domain model class. It it supposed to move the attribute information from the referenced class into the class that the MetadataType attribute has been set. But it doesn't do as advertised. What is causing the issue here? [MetadataType(typeof(ComponentModelMetaData))] public partial class Component { public int Id { get; set; } public string Name { get; set; } public ICollection<Repo> Repos { get; set; } public string Description { get; set; } }

EntityFramework SqlQuery does not work with custom mapping (DataAnnotation Column)

心不动则不痛 提交于 2019-12-25 11:36:08
问题 I'm using EF 5.0 (CodeFirst) with VS 2012 and am having trouble making a query using SqlQuery. The problem happens in the mapping between the property name of the entity and the name of the column in the database. My entity (model): [Table("Teste")] public class TesteEntity { [Column("Teste_Id")] public int Id { get; set; } [Required] public bool IsAdministrator { get; set; } [Required] public string Name { get; set; } } When I run the query, I get an error. Rotine: List<TesteEntity> list =

EntityFramework SqlQuery does not work with custom mapping (DataAnnotation Column)

痞子三分冷 提交于 2019-12-25 11:36:05
问题 I'm using EF 5.0 (CodeFirst) with VS 2012 and am having trouble making a query using SqlQuery. The problem happens in the mapping between the property name of the entity and the name of the column in the database. My entity (model): [Table("Teste")] public class TesteEntity { [Column("Teste_Id")] public int Id { get; set; } [Required] public bool IsAdministrator { get; set; } [Required] public string Name { get; set; } } When I run the query, I get an error. Rotine: List<TesteEntity> list =

ASP.net MVC3 DropDownListFor int Required DataAnnotation

本秂侑毒 提交于 2019-12-25 09:36:35
问题 Is there a way to trigger client-side validation for the select list created by DropDownListFor? The select list that gets created by the helper doesn't seem to get the "data-val" or "data-val-required" attributes that text inputs get for client-side validation. The validation does occur on the server-side when I check ModelState.IsValid, and the validation message is then displayed on the subsequent page load. I set a default option of "Please Select..." on the list because I want the user