data-annotations

Data Annotations with Entity Framework 5.0 (database first)

萝らか妹 提交于 2019-11-28 06:51:31
What is the best way to use data annotations for validation if I'm using an Entity Framework (v5.0) database first approach? This is my partial class created by Entity Framework: //------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using

What parameters does the stringlength attribute errormessage take?

浪子不回头ぞ 提交于 2019-11-28 06:46:10
In the MVC4 template one of the data annotation attributes used is stringlength. For example: [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] What parameters {0}, {1}, {2} (more?) are legal? Edit: To be more specific, I can see from the example and trial and error what the possibilities are, but I would like to see some hard documentation. I can't find anything about this in the StringLengthAttribute documentation . The {0} index is the display name of property, {1} is the MaximumLength , {2} is the MinimumLength . So, your error message

How do I specify the columns and rows of a multiline Editor-For in ASP.MVC?

喜你入骨 提交于 2019-11-28 06:40:23
In ASP.MVC 3, how do I specify the columns and rows for a multiline EditorFor (textarea)? I am using [UIHint("MultilineText")] , but can't find any documentation on how to add attributes for the text area. Desired HTML: <textarea cols="40" rows="10"></textarea> Relevant Part of my MVC 3 Model: [UIHint("MultilineText")] public string Description { get; set; } Relevant Part of my Razor cshtml: <div class="editor-field"> @Html.EditorFor(model => model.Description) </div> What I'm getting in View Source: <div class="editor-field"> <textarea class="text-box multi-line" id="Description" name=

How do Data Annotations work?

谁都会走 提交于 2019-11-28 06:21:54
I use Data Annotations in my ASP.NET MVC 3 project to validate the model. These are extremely convenient but currently they are magic to me. I read that data annotations do not throw exceptions. How then does MVC know to add validation errors to the model state dictionary? How does the failure to set a property on the model because of model validation bubble up to MVC if no exception is thrown? I always assumed that exceptions were thrown every time a property failed and that MVC model binding caught the exception and added it to the model state dictionary. To test this I created a console

Greater Than or Equal To Today Date validation annotation in MVC3

不打扰是莪最后的温柔 提交于 2019-11-28 05:23:59
Has anyone seen an MVC3 data annotation for Date validation that requires a single selected date to be equal to or greater than current date? If there's already a third party add on that's cool too. I'm already using the DataAnnotationsExtensions but it doesn't offer what I'm looking for. There doesn't seem to be any reference of this on. So, hoping someone has already solved this before I try to reinvent the wheel and write my own custom validator. I've tried Range but that requires 2 dates and both have to be constants in string format such as [Range(typeof(DateTime), "1/1/2011", "1/1/2016")

dataannotations set identity seed value on Primary Key with code first

筅森魡賤 提交于 2019-11-28 05:16:15
问题 ASP.NET MVC3 code first project. In my class definition how do I set the Identity seed value. public class Account { [Key] public int Id { get; set; } What would be the syntax to set the Identity seed to 1000000? Thank you 回答1: Thanks Craig, after looking at https://stackoverflow.com/a/5974656/968301 it was pretty simple. Create an intializer public class MyInitializer : DropCreateDatabaseIfModelChanges<MyContext> { protected override void Seed(MyContext context) { context.Database

How would you unit test data annotations?

我怕爱的太早我们不能终老 提交于 2019-11-28 04:21:35
问题 Two of the class properties have the following annotations: [Key] [Column] [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [MaxLength(25)] public string Name { get; set; } I understand that testing Key, Column and Required attributes is no longer a unit test, it's an integration test as it would depend on the underlying database, but how do you go about testing MaxLength(25) attribute? One of the alternatives that I can think of, is to add a code

Validating DataAnnotations with Validator class

余生颓废 提交于 2019-11-28 03:56:23
I'm trying to validate a class decorated with data annotation with the Validator class . It works fine when the attributes are applied to the same class. But when I try to use a metadata class it doesn't work. Is there anything I should do with the Validator so it uses the metadata class? Here's some code.. this works: public class Persona { [Required(AllowEmptyStrings = false, ErrorMessage = "El nombre es obligatorio")] public string Nombre { get; set; } [Range(0, int.MaxValue, ErrorMessage="La edad no puede ser negativa")] public int Edad { get; set; } } this doesnt work: [MetadataType

“The Id field is required” validation message on Create; Id not set to [Required]

╄→гoц情女王★ 提交于 2019-11-28 03:53:26
This is happening when I try to create the entity using a Create style action in Asp.Net MVC 2. The POCO has the following properties: public int Id {get;set;} [Required] public string Message {get; set} On the creation of the entity, the Id is set automatically, so there is no need for it on the Create action. The ModelState says that "The Id field is required", but I haven't set that to be so. Is there something automatic going on here? EDIT - Reason Revealed The reason for the issue is answered by Brad Wilson via Paul Speranza in one of the comments below where he says (cheers Paul): You're

change validate message in data annotation

心不动则不痛 提交于 2019-11-28 03:47:25
问题 my object has field with data type int. when i put in html form in this textbox letter not number the validator say- The field must be a number. how can i change this messages like this [Required(ErrorMessage = "Введите название")] [DisplayName("Название")] public int age { get; set; } 回答1: I haven't found a clean way to achieve this using Data Annotations. One way would be to write a custom model binder but this seems like a lot of work to do for such a simple task. Another way to achieve