data-annotations

DataAnnotation to compare two properties

我与影子孤独终老i 提交于 2019-11-30 01:34:32
Is there any way of using data annotations to compare two form field (eg. to confirm an email address) are the same, before allowing the form to be posted? eg. can the regular expression data annotation use the match function to reference another property in a ViewModel? Use the CompareAttribute public string EmailAddress {get; set;} [Compare(nameof(EmailAddress), ErrorMessage = "Emails mismatch")] public string VerifiedEmailAddress { get; set; } As one possibe option self-validation : Implement an interface IValidatableObject with method Validate , where you can put your validation code.

Validation of Guid

ⅰ亾dé卋堺 提交于 2019-11-30 01:23:15
问题 I have a strongly-typed view which has a DropDownListFor attribute on it. Each item in the dropdown list is represented by a GUID. What I'm after is a way to validate if a user selects an item from the dropdown list. At present i don't see anyway of doing this using Data Annotations. Is there anyway of achieving this using Data Annotations so client and server side validation would work. I'm guessing i need to make a custom method to do this but was wondering if anything already existed. 回答1:

Using DataAnnotations on Windows Forms project

元气小坏坏 提交于 2019-11-30 00:21:14
I recently used ASP.Net MVC with DataAnnotations and was thinking of using the same approach for a Forms project but I'm not sure how to go about it. I have set my attributes but they do not seem to get checked when I click Save. UPDATE: I have used Steve Sanderson's approach which will check for attributes on my class and return a collection of errors like so: try { Business b = new Business(); b.Name = "feds"; b.Description = "DFdsS"; b.CategoryID = 1; b.CountryID = 2; b.EMail = "SSDF"; var errors = DataAnnotationsValidationRunner.GetErrors(b); if (errors.Any()) throw new RulesException

Default resource for data annotations in ASP.NET MVC

若如初见. 提交于 2019-11-30 00:18:08
There's a way to set the default resource to the data annotations validations? I don't wanna make something like this: [Required(ErrorMessage="Name required.", ErrorMessageResourceType=typeof(CustomDataAnnotationsResources)] public string Name { get; set; } I would like something like this: Global.asax DataAnnotations.DefaultResources = typeof(CustomDataAnnotationsResources); then [Required] public string Name { get; set; } someone gimme a light! thanks in advance EDIT My real problem was with EF Code First CTP4. CTP5 fix it. Thanks for everyone. You could try doing this: Add this class

MVC 5 Remote Validation

こ雲淡風輕ζ 提交于 2019-11-29 23:04:01
I need to validate an input field value from user before the form is submitted. I have created an action in my custom controller and decorated the field with it: action name: CheckValue controller name: Validate [Remote("CheckValue", "Validate"), ErrorMessage="Value is not valid"] public string Value { get; set; } The problem is when I press submit, the form is being submitted and then the message Value is not valid is shown if the value entered by the user is not valid. How can I validate the value entered by user and prevent the form to be submitted if value is not valid, and display the

Server side validation with custom DataAnnotationsModelValidatorProvider

偶尔善良 提交于 2019-11-29 21:16:22
问题 I have setup a custom provider to allow setting validation attributes from a data store instead of in static code. Works great with the client side validation in my .NET MVC 4 project, but I am unable to get the server side validation to work. CustomModelValidatorProvider .cs: public class CustomModelValidatorProvider : DataAnnotationsModelValidatorProvider { protected override IEnumerable GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable attributes) { // set

localize data annotations default messages ([Required] [StringLength] etc.)

时光总嘲笑我的痴心妄想 提交于 2019-11-29 20:43:29
if I decorate the properties of my ViewModels with attributes like this: public class Vm { [Required] [StringLength(35)] public string Name {get;set;} } I am going to get english validation messages: "this field is required" "The field Name must be a string with a maximum length of 35" how could I translate them ? Darin Dimitrov You could use the ErrorMessageResourceName property: [Required(ErrorMessageResourceName = "SomeResource")] [StringLength(30, ErrorMessageResourceName = "SomeOtherResource")] public string Name { get; set; } You may checkout this blog post for an example. UPDATE: In

DataAnnotations vs IDataErrorInfo

爱⌒轻易说出口 提交于 2019-11-29 19:22:35
问题 DataAnnotations vs IDataErrorInfo Pros and Cons of both? Benefits of one over the other? (especially related to MVC) 回答1: Late entry to the discussion as I do not want to start a new question. Where I am coming from is to determine the best practice to apply to a medium size ASP.NET MVC project. Let me first summarise our options :- 1) IDataErrorInfo is simple to implement. All you need is to derive IDataErrorInfo in your Model class. The catch is that you are letting your model binding to be

Custom Validation Attribute MVC2

我们两清 提交于 2019-11-29 19:16:56
问题 I have a custom validation attribute which checks to see if two properties have the same values or not (like password and retype password): [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class EqualToPropertyAttribute : ValidationAttribute { public string CompareProperty { get; set; } public EqualToPropertyAttribute(string compareProperty) { CompareProperty = compareProperty; ErrorMessage = string.Format(Messages.EqualToError,

DisplayFormat.DataFormatString for a phone number or social security number

送分小仙女□ 提交于 2019-11-29 18:30:54
问题 Is there a way I can use the DisplayFormat attribute on a view model property to apply a DataFormatString format for a social security number or a phone number? I know I could do this with javascript, but would prefer to have the model handle it, if possible. [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "???????")] public string Ssn { get; set; } 回答1: The following should work, however notice the type difference for the Ssn property. [DisplayFormat(DataFormatString = "{0:##