data-annotations

Int or Number DataType for DataAnnotation validation attribute

主宰稳场 提交于 2019-11-28 03:07:32
On my MVC3 project, I store score prediction for football/soccer/hockey/... sport game. So one of properties of my prediction class looks like this: [Range(0, 15, ErrorMessage = "Can only be between 0 .. 15")] [StringLength(2, ErrorMessage = "Max 2 digits")] [Remote("PredictionOK", "Predict", ErrorMessage = "Prediction can only be a number in range 0 .. 15")] public int? HomeTeamPrediction { get; set; } Now, I need also change error message for a data type, int in my case. There is some default one used - "The field HomeTeamPrediction must be a number.". Need to find a way how to change this

Fluent Validation vs. Data Annotations [closed]

二次信任 提交于 2019-11-28 03:07:27
What are the operative differences between these two validation packages when used for ASP.NET MVC validatation? They seem to have similar objects, all the way to their object names. Is one related to another? What are their differences? In what way do these differences denote different use cases? Fluent Validation (3rd party solution) Data annotations (Microsoft "baked-in") Darin Dimitrov I prefer Fluent Validation : It gives me far better control of my validation rules Doing conditional validation on different properties is so much easier compared to Data Annotations It separates the

ReadOnly attribute doesn't work in ASP.NET MVC Models

♀尐吖头ヾ 提交于 2019-11-28 02:39:18
问题 I have marked a property as readonly in the model class, like this: public class RegisterModel { [Display(Name = "User name")] [ReadOnly(true)] public string UserName { get; set; } ... } and in my view: @Html.EditorFor(m => m.UserName) but when I run the application, the textbox is not readonly. I know I can use html attributes in the view to make it readonly, but I would prefer if this can be done in the model class itself. Can it be achieved? 回答1: [Update] I don't think it is possible

Data Annotation attributes are not firing in WCF

六眼飞鱼酱① 提交于 2019-11-28 02:07:16
问题 I am trying to validate the WCF service request using System.ComponentModel.DataAnnotations.dll of Version v4.0.30319. I am using VS2010 with Target Framework v4.0 . Below are my sample request(s). If i invoke the service operation using WcfTestclient the annotations are not firing even if i pass the invalid values ( null / String.Empty / "" ) for Name . Request1 : [MessageContract] public class AddUserRequest { [MessageBodyMember] [Required(ErrorMessage = "Id is required.")] public int Id {

Creating entity relationship with renamed fields and non-primary key in primary table

瘦欲@ 提交于 2019-11-28 01:50:55
The following are two partial tables in which I am trying to define a foreign key relationship. public class Form { [Key, Column("FormID")] public System.Guid FormGUID { get; set; } [Column("PatGUID")] public Nullable<System.Guid> PatientGUID { get; set; } } public class Patient { [Column("PatGUID")] public System.Guid PatientGUID { get; set; } [Key, Column("PatID")] public int PatientID { get; set; } } I've eliminated all but the relevant information, fields, navigations, etc. for this example; hopefully not too much. We have a table Form, with a FK of PatGUID to a Patient table with field

Property-level validation errors hinder the validation of Class-level validation

余生颓废 提交于 2019-11-28 00:57:55
问题 Update after Bounty was awarded A new solution is coming up to this problem. Please refer to ASP.NET MVC 3 Preview 1 here: http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx Look in the section Model Validation Improvements , where you will see the solution to my problem. Original Post Referring to my earlier post How to validate two properties with ASP.NET MVC 2 where I asked how I could compare two properties for Model validation. I did find the

Uppercase attribute that converts the input to uppercase

*爱你&永不变心* 提交于 2019-11-28 00:37:35
问题 I am working in MVC4 and want to define a model using an Uppercase attribute. The idea would be that the presence of the Uppercase attribute would cause the model value to be converted to uppercase when it arrived at the server. At the moment I have the following code within the model: [Required] [Display(Name="Account Code")] [StringValidation(RegExValidation.AccountCode, Uppercase=true)] public string Account { get { return _account; } set { if (value != null) _account = value.ToUpper(); }

mvc [DataType(DataType.EmailAddress) no validation

淺唱寂寞╮ 提交于 2019-11-27 21:32:01
问题 I'm using this code on an email field: [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email address")] public string Email { get; set; } [DataType(DataType.EmailAddress)] does not work (validation does not occur no at a server not on the client side). I am not sure if I should implement myself a Custom Attribute or I can use one included with MVC 3. Could you please suggest me a solution for creating a custom attribute in case I need to. I read also about some additional

ASP.NET MVC datetime culture issue when passing value back to controller

丶灬走出姿态 提交于 2019-11-27 21:18:27
How can i tell my controller/model what kind of culture it should expect for parsing a datetime? I was using some of this post to implement jquery datepicker into my mvc application. When i submit the date it gets "lost in translation" i'm not using the US formatting for the date, so when it gets sent to my controller it simply becomes null. I have a form where the user chooses a date: @using (Html.BeginForm("List", "Meter", FormMethod.Get)) { @Html.LabelFor(m => m.StartDate, "From:") <div>@Html.EditorFor(m => m.StartDate)</div> @Html.LabelFor(m => m.EndDate, "To:") <div>@Html.EditorFor(m => m

How can I set a RegularExpression data annotation's regular expression argument at runtime?

风流意气都作罢 提交于 2019-11-27 21:12:04
问题 We manage several ASP.NET MVC client web sites, which all use a data annotation like the following to validate customer email addresses (I haven't included the regex here, for readability): [Required(ErrorMessage="Email is required")] [RegularExpression(@"MYREGEX", ErrorMessage = "Email address is not valid")] public string Email { get; set; } What I would like to do is to centralise this regular expression, so that if we make a change to it, all of the sites immediately pick it up and we don