data-annotations

Custom RegularExpressionAttribute missing data-val-regex-pattern for client-side validation

陌路散爱 提交于 2019-12-01 06:05:40
I have created the following custom RegularExpressionAttribute [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class AlphaNumericAttribute: RegularExpressionAttribute, IClientValidatable { public AlphaNumericAttribute() : base("^[-A-Za-z0-9]+$") { } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), ValidationType = "alphanumeric" }; } } The field in the ViewModel is

Is it possible to toggle Validation Data Annotations on/off in MVC 3?

心已入冬 提交于 2019-12-01 05:03:22
问题 I have two separate VIEWS accessing the same MODEL. When I put the validator data annotations on the model, it works as advertised and prevents the data from being submitted (for both views) if left blank or not within range. However, I have one view that should be able to allow empty or null values to be saved for a property whereas another view needs to require information to be entered or selected before it lets it through. In other words, I'd like to turn off the validator on the property

ASP.NET MVC custom multiple fields validation

旧巷老猫 提交于 2019-12-01 04:51:51
问题 I'm developing an ASP.NET MVC 5.2.3 custom data annotation for validation in Visual Studio 2015. It needs to take any number of fields and ensure that if one has a value, they all must have a value; if they're all null/blank, it should be okay. A few examples have helped: ASP.NET MVC implement custom validator use IClientValidatable MVC Form Validation on Multiple Fields http://www.macaalay.com/2014/02/24/unobtrusive-client-and-server-side-age-validation-in-mvc-using-custom-data-annotations/

Custom RegularExpressionAttribute missing data-val-regex-pattern for client-side validation

吃可爱长大的小学妹 提交于 2019-12-01 04:18:06
问题 I have created the following custom RegularExpressionAttribute [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class AlphaNumericAttribute: RegularExpressionAttribute, IClientValidatable { public AlphaNumericAttribute() : base("^[-A-Za-z0-9]+$") { } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { yield return new ModelClientValidationRule { ErrorMessage =

C# Data Annotations in Interface

允我心安 提交于 2019-12-01 03:47:28
Quick question... If I put a notation in the Interface... Say [Required] can I ommit that notation in the C# class for the property? i.e. can I... Interface IFoo { [Required] string Bar {get; set;} } Class Foo : IFoo { string Bar {get; set;} } or do I need to just not put the notation in the interface and do this... Interface IFoo { string Bar {get; set;} } Class Foo : IFoo { [Required] string Bar {get; set;} } Dan Teesdale Placing the Data Annotation in the interface won't work. In the following link there is an explanation as to why: http://social.msdn.microsoft.com/Forums/en-US/adonetefx

Is it possible to use Data Annotations to validate parameters passed to an Action method of a Controller?

混江龙づ霸主 提交于 2019-12-01 03:38:18
I am using Data Annotations to validate my Model in ASP.NET MVC. This works well for action methods that has complex parameters e.g, public class Params { [Required] string Param1 {get; set;} [StringLength(50)] string Param2 {get; set;} } ActionResult MyAction(Params params) { If(ModeState.IsValid) { // Do Something } } What if I want to pass a single string to an Action Method (like below). Is there a way to use Data Annotations or will I have to wrap the string into a class? ActionResult MyAction(string param1, string param2) { If(ModeState.IsValid) { // Do Something } } Tejs I don't believe

C# Data Annotations in Interface

对着背影说爱祢 提交于 2019-12-01 00:57:22
问题 Quick question... If I put a notation in the Interface... Say [Required] can I ommit that notation in the C# class for the property? i.e. can I... Interface IFoo { [Required] string Bar {get; set;} } Class Foo : IFoo { string Bar {get; set;} } or do I need to just not put the notation in the interface and do this... Interface IFoo { string Bar {get; set;} } Class Foo : IFoo { [Required] string Bar {get; set;} } 回答1: Placing the Data Annotation in the interface won't work. In the following

Removing Required Attribute from Class but MVC3 still won't post the form without a value in the text box

孤街醉人 提交于 2019-12-01 00:28:51
问题 I have a class. At one point, I had set the properties of the class to [Required] using System.ComponentModel.... Okay, then I realized this was not needed. I have removed the required property but when I try to submit the form to an ActionResult the form does NOT post and still is trying to enforce the TextBoxFor(theModelProperty) to be populated. I have deleted the "obj" folder, the "bin" folder, and also "Cleaned" the solutions. Still NO resolution. I don't want to do a stupid workaround,

DataAnnotations on public fields vs properties in MVC

孤者浪人 提交于 2019-11-30 23:26:54
Why don't DataAnnotations work on public fields? Example: namespace Models { public class Product { [Display(Name = "Name")] public string Title; // { get; set; } } } public ActionResult Test() { return View(new Models.Product() { Title = "why no love?" }); } @Html.LabelFor(m => m.Title) // will return 'Title' if field, or 'Name' if property @Html.DisplayFor(m => m.Title) If Title is a field, then the Display attribute seems to have no effect. If Title is changed to a property, it works as expected as displays "Name". It would seem easy in this example to just change to a property, but I am

Is it possible to use LabelFor for header row in Index view

荒凉一梦 提交于 2019-11-30 21:30:02
I am trying to leverage DataAnnotation values in ASP.NET MVC Index view. Interesting, code generator uses field names (eg., BlogPost) as opposed to Html.LabelFor(m => Model.ColumNames["BlogPost"]) or something similar (I made it up). Does it mean, that if the Model is IEnumerable (as in Index view), then it is not possible to get to the Display Name that is specified by DataAnnotation? Hard to believe... My code is currently a mix of MVC 2 and MVC 3, if it makes any difference. You should, at the very least, be able to get what you want by using a partial view (ascx for WebForms) or a Display