data-annotations

How to handle Booleans/CheckBoxes in ASP.NET MVC 2 with DataAnnotations?

眉间皱痕 提交于 2019-11-27 17:17:32
I've got a view model like this: public class SignUpViewModel { [Required(ErrorMessage = "Bitte lesen und akzeptieren Sie die AGB.")] [DisplayName("Ich habe die AGB gelesen und akzeptiere diese.")] public bool AgreesWithTerms { get; set; } } The view markup code: <%= Html.CheckBoxFor(m => m.AgreesWithTerms) %> <%= Html.LabelFor(m => m.AgreesWithTerms)%> The result: No validation is executed. That's okay so far because bool is a value type and never null. But even if I make AgreesWithTerms nullable it won't work because the compiler shouts "Templates can be used only with field access, property

client-side validation trips on DataAnnotation Range attribute

风格不统一 提交于 2019-11-27 15:37:49
I have the following code in my Model class: [Range(1, 100)] public decimal Price { get; set; } After recent upgrade (I assume) of jquery.validate to 1.11.0, I am getting an error even if I enter valid value. If I turn off client validation in web.config - works fine. All other attributes (StringLength, Required) work fine. Generated HTML is the following (line breaks added for clarity): <input class="text-box single-line" data-val="true" data-val-number="The field Price must be a number." data-val-range="The field Price must be between 1 and 100." data-val-range-max="100" data-val-range-min=

Compare Dates DataAnnotations Validation asp.net mvc

依然范特西╮ 提交于 2019-11-27 15:15:25
问题 Lets say I have a StartDate and an EndDate and I wnt to check if the EndDate is not more then 3 months apart from the Start Date public class DateCompare : ValidationAttribute { public String StartDate { get; set; } public String EndDate { get; set; } //Constructor to take in the property names that are supposed to be checked public DateCompare(String startDate, String endDate) { StartDate = startDate; EndDate = endDate; } public override bool IsValid(object value) { var str = value.ToString(

Opposite of [compare(“ ”)] data annotation in .net?

一曲冷凌霜 提交于 2019-11-27 14:23:25
What is the opposite/negate of [Compare(" ")] data annotation" in ASP.NET? i.e: two properties must hold different values. public string UserName { get; set; } [Something["UserName"]] public string Password { get; set; } Leniel Maccaferri You can use the [NotEqualTo] data annotation operator included in MVC Foolproof Validation . I used it right now and it works great! MVC Foolproof is an open source library created by @nick-riggs and has a lot of available validators. Besides doing server side validation it also does client side unobtrusive validation. Full list of built in validators you get

WPF TextBox MaxLength — Is there any way to bind this to the Data Validation Max Length on the bound field?

自作多情 提交于 2019-11-27 14:01:17
问题 ViewModel: public class MyViewModel { [Required, StringLength(50)] public String SomeProperty { ... } } XAML: <TextBox Text="{Binding SomeProperty}" MaxLength="50" /> Is there any way to avoid setting the MaxLength of the TextBox to match up my ViewModel (which could change since it is in a different assembly) and have it automatically set the max length based on the StringLength requirement? 回答1: I used a Behavior to connect the TextBox to its bound property's validation attribute (if any).

Is the DataTypeAttribute validation working in MVC2?

牧云@^-^@ 提交于 2019-11-27 13:58:17
As far as I know the System.ComponentModel.DataAnnotations.DataTypeAttribute not works in model validation in MVC v1. For example, public class Model { [DataType("EmailAddress")] public string Email {get; set;} } In the codes above, the Email property will not be validated in MVC v1. Is it working in MVC v2? LukLed [DataType("EmailAddress")] doesn't influence validation by default. This is IsValid method of this attribute (from reflector): public override bool IsValid(object value) { return true; } This is example of custom DataTypeAttribute to validate Emails (taken from this site http:/

Using DataAnnotations to compare two model properties

人盡茶涼 提交于 2019-11-27 13:52:40
How would I go about writing a custom ValidationAttribute that compares two fields? This is the common "enter password", "confirm password" scenario. I need to be sure the two fields are equal and to keep things consistent, I want to implement the validation via DataAnnotations. So in pseudo-code, I'm looking for a way to implement something like the following: public class SignUpModel { [Required] [Display(Name = "Password")] public string Password { get; set; } [Required] [Display(Name = "Re-type Password")] [Compare(CompareField = Password, ErrorMessage = "Passwords do not match")] public

ASP.Net MVC: Can you use Data Annotations / Validation with an AJAX / jQuery call?

帅比萌擦擦* 提交于 2019-11-27 13:23:14
Can you use Data Annotations / Validation with an AJAX / jQuery call? If so, please provide an example or a post which shows an example. Basically I have seen an example of how to use Data Annotations, however it was with a full post back. Is there a way to do with an AJAX / jQuery call? Not sure how you would do this since I am not sure how you would construct the Model object on the client side. (I assume this is what you would have to do.) Someone told me this can be done, but I just don't understand how it can be. Thanks for your help. If you use Html.AjaxForm (instead of Html.BeginForm)

IValidatableObject Validate method firing when DataAnnotations fails

↘锁芯ラ 提交于 2019-11-27 13:21:56
I've a ViewModel which has some DataAnnotations validations and then for more complex validations implements IValidatableObject and uses Validate method. The behavior I was expecting was this one : first all the DataAnnotations and then, only if there were no errors, the Validate method. How ever I find out that this isn't always true. My ViewModel (a demo one) has three fileds one string , one decimal and one decimal? . All the three properties have only Required attribute. For the string and the decimal? the behavior is the expected one, but for the decimal , when empty, Required validation

.NET MVC Custom Date Validator

心不动则不痛 提交于 2019-11-27 12:42:28
问题 I'll be tackling writing a custom date validation class tomorrow for a meeting app i'm working on at work that will validate if a given start or end date is A) less than the current date, or B) the start date is greater than the end date of the meeting (or vice versa). I think this is probably a fairly common requirement. Can anyone point me in the direction of a blog post that might help me out in tackling this problem? I'm using .net 3.5 so i can't use the new model validator api built into