data-annotations

Data Annotations for validation, at least one required field?

南笙酒味 提交于 2019-11-26 19:57:30
问题 If I have a search object with a list of fields, can I, using the System.ComponentModel.DataAnnotations namespace, set it up to validate that at least one of the fields in the search is not null or empty? i.e All the fields are optional but at least one should always be entered. 回答1: I'd create a custom validator for this - it won't give you client side validation, just server side. Note that for this to work, you'll need to be using nullable types, as value types will default to 0 or false :

Providing localized error messages for non-attributed model validation in ASP.Net MVC 2?

北慕城南 提交于 2019-11-26 19:45:22
问题 I'm using the DataAnnotations attributes along with ASP.Net MVC 2 to provide model validation for my ViewModels: public class ExamplePersonViewModel { [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Validation))] [StringLength(128, ErrorMessageResourceName = "StringLength", ErrorMessageResourceType = typeof(Resources.Validation))] [DataType(DataType.Text)] public string Name { get; set; } [Required(ErrorMessageResourceName = "Required",

displayname attribute vs display attribute

旧城冷巷雨未停 提交于 2019-11-26 19:40:20
What is difference between DisplayName attribute and Display attribute in ASP.NET MVC? Spock They both give you the same results but the key difference I see is that you cannot specify a ResourceType in DisplayName attribute. For an example in MVC 2, you had to subclass the DisplayName attribute to provide resource via localization. Display attribute (new in MVC3 and .NET4) supports ResourceType overload as an "out of the box" property. Darin Dimitrov DisplayName sets the DisplayName in the model metadata. For example: [DisplayName("foo")] public string MyProperty { get; set; } and if you use

DataAnnotation for Required property

大兔子大兔子 提交于 2019-11-26 19:34:59
问题 First it works, but today it failed! This is how I define the date property: [Display(Name = "Date")] [Required(ErrorMessage = "Date of Submission is required.")] [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] [DataType(DataType.Date)] public DateTime TripDate { get; set; } It has been working in the past. But today, when I call the same ApiController action: [HttpPost] public HttpResponseMessage SaveNewReport(TripLeaderReportInputModel model) The Firebug reports:

Get [DisplayName] attribute of a property in strongly-typed way

こ雲淡風輕ζ 提交于 2019-11-26 18:56:52
问题 Good day! I've such method to get [DisplayName] attribute value of a property (which is attached directly or using [MetadataType] attribute). I use it in rare cases where I need to get [DisplayName] in controller code. public static class MetaDataHelper { public static string GetDisplayName(Type dataType, string fieldName) { // First look into attributes on a type and it's parents DisplayNameAttribute attr; attr = (DisplayNameAttribute)dataType.GetProperty(fieldName).GetCustomAttributes

Custom model validation of dependent properties using Data Annotations

拟墨画扇 提交于 2019-11-26 18:40:40
Since now I've used the excellent FluentValidation library to validate my model classes. In web applications I use it in conjunction with the jquery.validate plugin to perform client side validation as well. One drawback is that much of the validation logic is repeated on the client side and is no longer centralized at a single place. For this reason I'm looking for an alternative. There are many examples out there showing the usage of data annotations to perform model validation. It looks very promising. One thing I couldn't find out is how to validate a property that depends on another

Can I create a generic method that takes a value type or a reference type but always returns a nullable type

大憨熊 提交于 2019-11-26 18:30:00
问题 This is my method. Note that I am returning the equivalent nullable type for the generic parameter R : public static Nullable<R> GetValue<T, R>(this T a, Expression<Func<T, R>> expression) where T : Attribute where R : struct { if (a == null) return null; PropertyInfo p = GetProperty(expression); if (p == null) return null; return (R)p.GetValue(a, null); } I can use it in a call to get the value of an attribute like this: //I don't throw exceptions for invalid or missing calls //because I

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

江枫思渺然 提交于 2019-11-26 18:23:08
问题 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; } 回答1: 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

Is the DataTypeAttribute validation working in MVC2?

亡梦爱人 提交于 2019-11-26 18:21:28
问题 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? 回答1: [DataType("EmailAddress")] doesn't influence validation by default. This is IsValid method of this attribute (from reflector): public override bool IsValid(object value) { return

Validation does not work when I use Validator.TryValidateObject

淺唱寂寞╮ 提交于 2019-11-26 18:20:49
问题 DataAnnotations does not work with buddy class. The following code always validate true. Why ? var isValid = Validator.TryValidateObject(new Customer(), Context, results, true); and here is the buddy class. public partial class Customer { public string Name { get; set; } public int Age { get; set; } } [MetadataType(typeof(CustomerMetaData))] public partial class Customer { public class CustomerMetaData { [Required(ErrorMessage = "You must supply a name for a customer.")] public string Name {