data-annotations

Client-side validation not firing for CompareAttribute DataAnnotation

三世轮回 提交于 2019-12-04 07:50:16
I'm laying out a view that compares two password strings. The two properties in one of my models are pretty straightforward: [Required] [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")] [StringLength(20, MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "New Password")] public string NewPassword { get; set; } [Required] [DataType(DataType.Password)] [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")] [StringLength(20, MinimumLength = 6)] [Display(Name = "Confirm Password")] [Compare("NewPassword", ErrorMessage = "The new password

Unobtrusive Validation for Inline Datepicker not working, works fine for non-inline Datepicker

Deadly 提交于 2019-12-04 07:32:08
I didn't use jquery.ui's datepicker because I needed to select an entire week and multiple dates under different conditions, so I am using Keith Wood's datepicker in an EditorTemplate for my Date fields: @model Nullable<DateTime> @{ string name = ViewData.TemplateInfo.HtmlFieldPrefix; string id = name.Replace(".", "_"); string dt = Model.HasValue ? String.Format("{0:d}",(string)Model.Value.ToShortDateString()) : string.Empty; } @Html.TextBox("", dt, new { @class = "datefield", @type = "date", @id = id }) <script type="text/javascript"> $(document).ready(function () { $('#@id').datepick({

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

爱⌒轻易说出口 提交于 2019-12-04 06:36:53
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 not valid for The Date. One of the first things that jumps out at me about that message is that it is

Data Annotations, IDataErrorInfo and MVVM

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 06:02:17
问题 I'm trying to find the best way to validate data in MVVM. Currently, I'm trying to use IDataErrorInfo with Data Annotations using the MVVM pattern. However, nothing seems to work and I'm not sure what I could be doing wrong. I have something like this. Model public class Person : IDataErrorInfo { [Required(ErrorMessage="Please enter your name")] public string Name { get; set; } public string Error { get { throw new NotImplementedException(); } } string IDataErrorInfo.this[string propertyName]

How can I ignore missing fields from the MetadataType attribute?

时光怂恿深爱的人放手 提交于 2019-12-04 05:17:28
问题 I have DTOs that are mapped to ViewModels. To avoid having to manage validation attributes (and other attributes), I wanted to write the validation attributes for all the properties on a single class and reuse it on my ViewModels. However, when I try to use the Metadata on a ViewModel that does not have all the properties of the DTO (all of them really...), it gives me an System.InvalidOperationException exception. Exception: Le type de métadonnées associé pour le type 'MyProject

Applying Data Annotations to sub properties of the View Model in MVC?

六眼飞鱼酱① 提交于 2019-12-04 04:30:04
Putting simple Data Annotations on properties is great, public class UnicornViewModel { [Required] public string Name { get; set; } But lets say I'm have something like this: public class SuperPower { public class Name { get; set; } } public class UnicornViewModel { [Required] public string Name { get; set; } public SuperPower PrimarySuperPower { get; set; } public SuperPower SecondarySuperPower { get; set; } How do I apply the Required attribute on PrimarySuperPower.Name while leaving it optional for SecondarySuperPower.Name? Preferably 1. something that ties into client side validation and 2

Cultural specific data annotations

雨燕双飞 提交于 2019-12-04 04:27:35
问题 I'm trying to get cultural specific data annotations. [DisplayFormat(DataFormatString = "{0:d}")] public DateTime Date{ get; set; } I thought this would work. So in the us it would show DD/MM/yyyy and in europe it would show MM/DD/YYYY. To test this, I set my default chrome language to English (UK) and restarted the browser. I'm still getting the US format though, which leads me to believe my DataFormatString isn't respecting cultures. How to I fix this? Can I also cut of the year so it's

Data annotations MVC3 Required attribute

和自甴很熟 提交于 2019-12-04 04:15:34
问题 I have the Model (User) below, I use it to add new users and to update existing users. When I'm adding a new user it's required to enter the user name and the password, and when I'm updating it's required to enter only the user name because it's not allowed to change the password. Here is the problem, adding a new user everything works ok because I enter both name and password values so ModelState.IsValid returns true, but when updating an user there is no input to the password, so it always

Code first columns with type char(36)

我的梦境 提交于 2019-12-04 03:53:38
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? If you want to keep with Data Annotations, then just simply use: [StringLength( 36 )] [Column( TypeName = "char" )] public string UserIdentifier{ get; set;

Entity Framework 4.1 Code First - Define many-to-many using data annotations only

时间秒杀一切 提交于 2019-12-04 03:41:58
Is it possible to define a many-to-many relationship in Entity Framework 4.1 (Code First approach) using Data Annotations only, without model builder? For example, something like: Product = { Id, Name, ... } Category = { Id, Name, ... } ProductCategory = { ProductId, CategoryId } You get the picture. I don't want to have an intermediate entity ProductCategory in the context with two many-to-ones since I don't have any additional data, just the two FKs. Also, I should be able to define table name for the intermediate table for use with an existing database. It is possible to define many-to-many