data-annotations

How to write custom RegularExpressionValidator which takes the values from the config file?

我是研究僧i 提交于 2019-12-03 12:35:35
I have to use a Regular expression validator for username property in my model. I am getting this regular expression from the config file. [RegularExpression(UsernameValidationExpression)] //UsernameValidationExpression = value from the config file public string UserName { get; set; } Here i am getting an error "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type" How can i solve this?? public class ConfigRegularExpressionAttribute : RegularExpressionAttribute { public ConfigRegularExpressionAttribute(string

Custom Model Binder Not Validating Model

半世苍凉 提交于 2019-12-03 12:01:23
I started to play around with knockout.js and in doing so I used the FromJsonAttribute (created by Steve Sanderson). I ran into an issue with the custom attribute not performing model validation. I put together a simple example-- I know it looks like a lot of code-- but the basic issue is how to force the validation of the model within a custom model binder. using System.ComponentModel.DataAnnotations; namespace BindingExamples.Models { public class Widget { [Required] public string Name { get; set; } } } and here is my controller: using System; using System.Web.Mvc; using BindingExamples

How can I use DisplayName data annotations for column headers in WebGrid?

为君一笑 提交于 2019-12-03 11:53:34
问题 I have a Car class that I'm trying to display in an MVC 3 view using the WebGrid helper. Below are the Car and it's metadata class. Car class: [MetadataType(typeof(CarMetadata))] public partial class Car { // car implementation } Car metadata class: public class CarMetadata { [DisplayName("Car Name")] [StringLength(100, ErrorMessageResourceType = typeof(ValidationText), ErrorMessageResourceName="CarNameDescriptionLength")] [Required] public string CarName { get; set; } } View contents: @model

MVC 4 - DataAnnotations - Validation for Type

孤者浪人 提交于 2019-12-03 11:09:23
I have the following code working [Required(ErrorMessage = "Price is required.")] [Range(typeof(Decimal), "1", "9999", ErrorMessage = "Price xx.xx")] public decimal? productPrice { get; set; } When the page is submitted with Price = EMPTY Field error message is "Price is required.". Price = over 9999 error message is "Price xx.xx". However, when I type 'aaaa' the error message is "The field productPrice must be a number." How can I change the message if type in not correct? Like : "Price must be a decimal/number between 1-9999. ---- UPDATE: ---- The below code worked with NULL, Not Decimal,

DataAnnotations “NotRequired” attribute

不羁的心 提交于 2019-12-03 11:05:59
I've a model kind of complicated. I have my UserViewModel which has several properties and two of them are HomePhone and WorkPhone . Both of type PhoneViewModel . In PhoneViewModel I have CountryCode , AreaCode and Number all strings. I want to make the CountryCode optional but AreaCode and Number mandatory. This works great. My problem is that in the UserViewModel WorkPhone is mandatory, and HomePhone is not. Is there anyway I can dissable Require attributs in PhoneViewModel by setting any attributes in HomeWork property? I've tried this: [ValidateInput(false)] but it is only for classes and

The DataAnnotations [Phone] Attribute

血红的双手。 提交于 2019-12-03 10:16:39
What is the default, valid format of the [Phone] attribute? In the data table, the phone column is navrchar (16) If I enter a phone # like 1112223333, I get "field is not a valid phone number." If I enter 01112223333, I get "The value '11112223333' is invalid." Also, how to override it? I understand that I could do something like this, but is this the best practice in this case? [RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}",ErrorMessage="Invalid Phone Number!")] Related code: [Required] [Phone] public string Phone { get; set; } <div class="editor-field"> @Html.EditorFor(model =>

ASP.NET MVC3 Custom Validation Message Behaviour

末鹿安然 提交于 2019-12-03 10:01:09
Coming from the asp.net webforms model I'm used to using validators which display an error in the form <span title="Username is Required">*</span> . I'm quite clear how the MVC3 validators work out of the box, so please no more answers explaining how validators work in MVC3 as I'm pretty sure I have that nailed. What I am trying to accomplish is have the validation error message showing as the title of the span tag as shown in the first paragraph. I have managed to replicate this in MVC3 but am not sure if the way I have done it follows best practise. I would appreciate any input as to whether

Compare (password) attribute

感情迁移 提交于 2019-12-03 08:43:30
问题 I'd like to create a view model for a new user using the code below. The "User" class contains just the two properties (simplified for now) that I will persist to the database; the view model adds a "compare password" field, which is only used in the view. I'd prefer to have the view model use the "User" class directly, rather than repeating all of the fields defined in "User". My question is how do I properly reference "User.Password" in the [Compare] attribute for the "ComparePassword"

Conditional data annotation

余生颓废 提交于 2019-12-03 08:33:42
问题 Is there a way to make a data annotation conditional? I have a table Party where I store both organisations and persons. If I'm adding an organisation I don't want the field surname to be required, but only if I'm adding a person. public class Party { [Required(ErrorMessage = "{0} is missing")] [DisplayName("Your surname")] public object surname { get; set; } [DisplayName("Type")] public object party_type { get; set; } ... } I'd like a condition for the required data annotation of surname,

Using Data Annotations on POCO's with MVC for Remote Validation

泄露秘密 提交于 2019-12-03 08:02:17
I am developing an ASP.NET MVC app and I've been looking into using Data Annotations on my POCO's which are defined in my Service Layer. As long as I have a reference to System.ComponentModel & System.ComponentModel.DataAnnotations this is no problem and what I like about this is that it allows me to reuse my Service Layer in a Win Forms app. I'm now looking to do some Remote Validation using Data Annotations and have taken a look at this article: http://msdn.microsoft.com/en-us/library/ff398048(VS.100).aspx However, to use the Data Annotations in this context I need to reference System.Web