data-annotations

ASP.NET MVC2 Model Validation Fails with Non-US Date Format

痞子三分冷 提交于 2019-12-06 00:44:43
问题 I have a small MVC2 app that displays in two cultures: en-US and es-MX. One portion contains a user input for a date that is pre-populated with the current date in the Model. When using en-US, the date field is displayed as MM/dd/yyyy and can be changed using the same format without causing any validation errors. When using es-MX, the date field is displayed as dd/MM/yyyy, but when the date is edited in this format, the server-side validation fails with the message: The value '17/05/1991' is

How to get DataAnnotations working in asp.net 4.5 WebForms?

一个人想着一个人 提交于 2019-12-05 23:06:39
I'm using .net 4.5 WebForms with Model Binding and Entity Framework 5. Part of my webform: <asp:ListView ID="MenteeModuleList" ItemPlaceholderID="itemPlaceHolder" OnCallingDataMethods="MenteeModuleList_CallingDataMethods" ItemType="PFA_Mentorship.BLL.MenteeModuleBL+MenteeModuleGrid" DataKeyNames="MenteeModuleID" SelectMethod="GetMenteeModulesGrid" InsertItemPosition="LastItem" InsertMethod="InsertMenteeModule" runat="server"> <LayoutTemplate> <table runat="server"> <tr runat="server"> <th id="Th1" runat="server">Module</th> <th id="Th2" runat="server">Start Date</th> <th id="Th3" runat="server

Why does EF Code First [InverseProperty] attribute fail to work when used with [ForeignKey] attribute?

喜夏-厌秋 提交于 2019-12-05 22:46:21
Using: EF 4.3.1, Visual Studio 2010, SQL CE 4.0 My understanding is that when declaring Foreign Keys with DataAnnotation in EF, it can be done either of the following ways: Option 1- [ForeignKey("Player1Home")] public long? HPlayer1Id { get; set; } public virtual Player Player1Home { get; set; } Option 2- public long? HPlayer1Id { get; set; } [ForeignKey("HPlayer1Id")] public virtual Player Player1Home { get; set; } Problem: When the InverseProperty DataAnnotation gets used with Option 2 an extra column gets generated in the database (Player1Home_Id) in addition to HPlayer1Id. [Table("Matches"

Custom ValidationAttribute doesn't work. Always returns true

喜你入骨 提交于 2019-12-05 22:31:57
I've created a custom ValidationAttribute class to check the age of a person in my application: 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; } } The data annotation is set on the field like this: [MinimumAge(MinAge = 18, ErrorMessage = "Person must be over the age of 18")] public

How to specify order of Data Annotation errors in Html.ValidationSummary

一笑奈何 提交于 2019-12-05 20:35:32
I'm displaying errors on my form with the use of <%= Html.ValidationSummary("Please review the errors below") %> My domain object inherits from a base class and I am finding that the base class data annotation properties are being displayed at the bottom of the list. This goes against the order in which they appear in my form. Is there any way of specifying what order the errors should be displayed? Example: public class ClassA { [Required]public string AProperty; } public class ClassB : ClassA { [Required]public string BProperty; } My form (strongly typed view of ClassB): AProperty: <%= Html

C# adding DataAnnotations to entities from the EntityFramework

淺唱寂寞╮ 提交于 2019-12-05 19:34:14
I'm using the ADO.Net Entity Framework. To handle input validation I'm trying to use DataAnnotations , I looked around on StavkOverflow and Google, and everywhere I found almost the same example of using MetadataType . However, I've been trying for hours and I cannot get it to work.. For some reason, the CustomAttributes from the EmployeeMetaData class are not being applied to the respective field/properties on the Employee class. Does anyone have any idea why this might be happening? And yes, I'm sure the property types and names match perfectly. Any help is appreciated, I've been stuck on

Code first columns with type char(36)

瘦欲@ 提交于 2019-12-05 19:20:17
问题 So I have a UserProfile model class as part of SimpleMembership. In it I need to store a legacy identifier that exists in another DB of type char(36) . I'd love to change this to something more sensible like a uniqueIdentifier but that's out of scope for today's activities. My current annotation creates a column nvarchar(36) [StringLength(36)] public string UserIdentifier{ get; set; } I'd like a column of char(36) instead. Is this possible? 回答1: If you want to keep with Data Annotations, then

One out of 2 properties should be null (EntityFramework Code First)

China☆狼群 提交于 2019-12-05 18:45:26
I've searched a lot in the forum, but haven't found anything about regarding this issue. I have 2 properties in the EntityFramework Code First: [Column(TypeName = "Money")] public decimal? Debit { get; set; } [Column(TypeName = "Money")] public decimal? Credit { get; set; } One of them should be not null, but the other one should be null Examples: Debit=null; Credit=34; Debit=45; Credit=null; On the other hand, it should not be possible to set both or none of them null. Is it possible to handle this issue with data annotations or should I solve it with a workaround? Best regards! You could

Is there any way to stop DataAnnotation validation after the first failure?

泄露秘密 提交于 2019-12-05 18:30:05
In my ViewModels I use several DataAnnotations to validate the form data, there are usually 2-3 annotations per field. For example a field for an email address might look like this: [Required(ErrorMessage = "Please enter an email address.")] [Email(ErrorMessage = "That is not a valid email address.")] // Custom public string Email { get; set; } Now if someone were to submit the form, both errors would show up in the validation summary. Is there any easy way to specify an order to run the validation annotations so that if the Required validation fails, the Email validation doesn't run? If this

Asp.net Web Api nested model validation

对着背影说爱祢 提交于 2019-12-05 17:23:20
I'm running in to a bit of a problem in asp.net web api's model binding and validation (via data annotations). It seems like if i have a model with property such as Dictionary<string, childObject> obj { get; set; } the childObject's validations don't seem to trigger. The data is bound from json with Json.Net serializer. Is there some workaround or fix to this? Or have I misunderstood something else related to this? I can't help but wonder why this doesn't result in errors: public class Child { [Required] [StringLength(10)] public string name; [Required] [StringLength(10)] public string desc; }