ASP.Net MVC DisplayFormat
The [DisplayFormat] attribute is only used in EditorFor/DisplayFor, and not by the raw HTML APIs like TextBoxFor.
The [DisplayFormat] attribute is only used in EditorFor/DisplayFor, and not by the raw HTML APIs like TextBoxFor.
Make sure that your project references system.web.mvc v3.xxxxx. Then your code should be something like this: using System.Web.Mvc; . . . . [Required(ErrorMessage = “This field is required.”)] public string NewPassword { get; set; } [Required(ErrorMessage = “This field is required.”)] [Compare(nameof(NewPassword), ErrorMessage = “Passwords don’t match.”)] public string RepeatPassword { get; set; }
You can use the DataAnnotations.Validator class, as described here: http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx But if you’re using a “buddy” class for the metadata, you need to register that fact before you validate, as described here: http://forums.silverlight.net/forums/p/149264/377212.aspx TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(myEntity), typeof(myEntityMetadataClass)), typeof(myEntity)); List<ValidationResult> results = new List<ValidationResult>(); ValidationContext context = new ValidationContext(myEntity, null, null) bool valid = Validator.TryValidateObject(myEntity, context, … Read more
Considerations after comments’ exchange: The consensual and expected behavior among developers is that IValidatableObject‘s method Validate() is only called if no validation attributes are triggered. In short, the expected algorithm is this (taken from the previous link): Validate property-level attributes If any validators are invalid, abort validation returning the failure(s) Validate the object-level attributes If … Read more
You can use the [NotEqualTo] data annotation operator included in MVC Foolproof Validation. I used it right now and it works great! MVC Foolproof is an open source library created by @nick-riggs and has a lot of available validators. Besides doing server side validation it also does client side unobtrusive validation. Full list of built … Read more
[DataType(“EmailAddress”)] doesn’t influence validation by default. This is IsValid method of this attribute (from reflector): public override bool IsValid(object value) { return true; } This is example of custom DataTypeAttribute to validate Emails (taken from this site http://davidhayden.com/blog/dave/archive/2009/08/12/CustomDataTypeAttributeValidationCustomDisplay.aspx): [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] public class EmailAddressAttribute : DataTypeAttribute { private readonly Regex regex = … Read more
Both these approaches should work: public class Product { public int ProductId { get; set; } public string Name { get; set; } [ForeignKey(“Category”)] public string CId { get; set; } public virtual Category Category { get; set; } } Or: public class Product { public int ProductId { get; set; } public string Name … Read more
There are two ways to do this: Models.Test test = new Models.Test(); string DisplayName = test.GetDisplayName(t => t.Name); string DisplayName = Helpers.GetDisplayName<Models.Test>(t => t.Name); The first one works by virtue of writing a generic extension method to any TModel (which is all types). This means it will be available on any object and not just … Read more
EDIT: I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx Before validating, you need to manually register the metadata class: TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Person), typeof(PersonMetadata)), typeof(Person)); List<ValidationResult> res = new List<ValidationResult>(); bool valid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), res, true); (Original answer follows) The problem isn’t specifically with your partial class, it’s that Validator.TryValidateObject doesn’t seem to recognize … Read more
Check The AccountMOdel in the default project of MVC2, There is an attribute PropertiesMustMatchAttribute applied to the ChangePasswordModel to validate that the NewPassword and ConfirmPassword Match [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class PropertiesMustMatchAttribute : ValidationAttribute { private const string _defaultErrorMessage = “‘{0}’ and ‘{1}’ do not match.”; private readonly object _typeId … Read more