data-annotations

why does ASP.Net MVC Range Attribute take a Type?

为君一笑 提交于 2019-12-05 05:42:20
I was just wondering why the Range validation attribute can take a Type and two strings as parameters? Is this for validating strings against an Enum or something like this? Also what I am trying to do is find an easy way to validate a 3 character string which must be present in an enum, any sugestions? Thanks, Alex. I did find the Range ctor you mentioned fishy. Couldn't help but investigate. (So I wrote this answer like a log while investigating.) From MSDN public RangeAttribute( Type type, string minimum, string maximum ) Note : MSDN says the Type should be IComparable . And, their example

Is it possible to reuse the DataAnnotations in ViewModel?

社会主义新天地 提交于 2019-12-05 04:41:32
问题 In my MVC application, I defined the DataAnnotations in the domain models. Although the DataAnnotations properties as Display, etc. can be retrieved when using Domain model, they cannot be retrieved when using the same properties on ViewModel and using this ViewModel. I think it is not seem to good to define the DataAnnotations in ViewModel again. So, is it possible or which way should I follow? Domain Model: public class Issue { [Key] public int ID { get; set; } [Required(ErrorMessage =

About Enum and DataAnnotation

倾然丶 夕夏残阳落幕 提交于 2019-12-05 04:30:35
I have this Enum (Notebook.cs): public enum Notebook : byte { [Display(Name = "Notebook HP")] NotebookHP, [Display(Name = "Notebook Dell")] NotebookDell } Also this property in my class (TIDepartment.cs): public Notebook Notebook { get; set; } It's working perfectly, I just have one "problem": I created an EnumDDLFor and it's showing the name I setted in DisplayAttribute, with spaces, but the object doesn't receive that name in DisplayAttribute, receives the Enum name (what is correct), so my question is: Is there a way to receive the name with spaces which one I configured in DisplayAttribute

MVC 4 razor Data Annotations ReadOnly

孤街浪徒 提交于 2019-12-05 03:57:51
The ReadOnly attribute does not seem to be in MVC 4. The Editable(false) attribute does not work the way I would want it to. Is there something similar that works? If not then how can I make my own ReadOnly attribute that would work like this: public class aModel { [ReadOnly(true)] or just [ReadOnly] string aProperty {get; set;} } so I can put this: @Html.TextBoxFor(x=> x.aProperty) instead of this ( which does work ): @Html.TextBoxFor(x=> x.aProperty , new { @readonly="readonly"}) or this ( which does work but values are not submitted ): @Html.TextBoxFor(x=> x.aProperty , new { disabled=

ASP.NET MVC4: An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type

允我心安 提交于 2019-12-05 03:30:27
I had the following code: [Required(ErrorMessage = MessageModel.translateMessage("required")))] [Display(Name= MessageModel.translateMessage("id"))] public string user_id { get; set; } I am trying to make the error message dynamic but I get error when compiled.: "An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type." Any solution for this issue? First you create a Resource .resx file this will contain your localised strings. When you declare the attribute you set the ResourceType argument. This causes the Name,

Custom validation error message if user puts a non-numeric string in an int field

这一生的挚爱 提交于 2019-12-05 03:29:42
This question has to have been asked before, but I think the search terms are too generic for me to find the answer I'm looking for, so I'll ask it again. I have a model with an int property, and a range annotation. If the user enters something other than an int, the validation message responds with The value '<bad data>' is not valid for '<property name>' ... which is great, but I want to provide a bit more feedback, i.e., Expecting an integer value in this field. . Since this validation fails before the other validators take a look, I don't know how (or if it's possible) to override the

separate numbers by comma with asp.net mvc

霸气de小男生 提交于 2019-12-05 03:27:55
I am working on an MVC2 appication. I use data annotations to validate data (both client side and server side). I have several fields in my model that only allows decimal values. As soon as a user types a decimal values I want it to be converted to comma seperated more readable format. For instance 1200 should be formatted to 1,200 while 500 should stay as it is. Here is my model: public virtual GrossFee? Fee { get; set; } And here is how it is on the view: %: Html.TextBoxFor(model => model.GrossFee)%> Any ideas regarding this will be highly appreciated. Thanks! Instead of the Html.TextBoxFor

Define markup for [Required] fields in View in ASP.NET MVC 2.0

戏子无情 提交于 2019-12-05 02:42:15
问题 We have a model with properties decorated with [Required] which works great for validation. However, what we'd like to do is mark those required fields in the view with an asterisk (or some other style) to denote they are required before the user enters any data for validation. I don't seem to be able to find anything built into the MVC framework to allow me to do this and therefore wondered if anyone else had already solved this and how? Ultimately what we're trying to prevent is the need to

How can I validate Mobile/Landline Number formatting using MVC.Net?

╄→гoц情女王★ 提交于 2019-12-05 02:15:12
问题 Pretty simple question - validating International Mobile/Landline Number using Data Annotations. Any suggestions? 回答1: There is no single "international" phone number standard, but you can do some validation. For example: [RegularExpression(@"^([\+]|0)[(\s]{0,1}[2-9][0-9]{0,2}[\s-)]{0,2}[0-9][0-9][0-9\s-]*[0-9]$")] This allows, for example: 044 123-456 , +2 12-12456 , +(234) 56-56-452 . See here for more kinds of regular expressions for phone numbers: http://regexlib.com/Search.aspx?k=phone

MVC3 DataAnnotationsExtensions error using numeric attribute

a 夏天 提交于 2019-12-05 01:34:10
问题 I've installed Scott's Kirkland DataAnnotationsExtensions. In my model I have: [Numeric] public double expectedcost { get; set; } And in my View: @Html.EditorFor(model => model.expectedcost) Now, when the page tries to render I get the following error: Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: number Any ideas why I'm getting the error ? 回答1: The quick answer is simply remove the attribute [Numeric] The