data-annotations

Data Annotation Ranges of Dates

点点圈 提交于 2019-11-27 04:26:29
问题 Is it possible to use [Range] annotation for dates? something like [Range(typeof(DateTime), DateTime.MinValue.ToString(), DateTime.Today.ToString())] 回答1: Docs on MSDN says you can use the RangeAttribute [Range(typeof(DateTime), "1/2/2004", "3/4/2004", ErrorMessage = "Value for {0} must be between {1} and {2}")] public datetime Something { get; set;} 回答2: I did this to fix your problem public class DateAttribute : RangeAttribute { public DateAttribute() : base(typeof(DateTime), DateTime.Now

MVC unobtrusive range validation of dynamic values

二次信任 提交于 2019-11-27 04:22:53
问题 I have a value on my model, that must fall within the range of two other values on my model. For example: public class RangeValidationSampleModel { int Value { get; set; } int MinValue { get; set; } int MaxValue { get; set; } } Of course, I can't pass these Min/MaxValues into my DataAnnotations attributes, as they have to be constant values. I'm sure I need to build my own validation attribute, but I haven't done this much and can't wrap my mind around how it should work. I've searched for

Why is DisplayFormat DataFormatString not working?

淺唱寂寞╮ 提交于 2019-11-27 04:22:39
I have a property in my view model as follows: [Editable(false)] [Display(Name = "Date")] [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)] public DateTime MovementDate { get; set; } Yet the markup <td> @Html.DisplayFor(modelItem => item.MovementDate) </td> renders the date value as 2013/05/15 12:00:00 AM . What am I doing wrong? My model: public class WithDateModel { [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")] public DateTime TheDate { get; set; } public WithDateModel() { TheDate = DateTime.Now; } } My view: @model ParkPay.WebTests.Models

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

本秂侑毒 提交于 2019-11-27 04:11:58
问题 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

Why can't I reference System.ComponentModel.DataAnnotations?

坚强是说给别人听的谎言 提交于 2019-11-27 04:01:41
I'm trying to use DataAnnotations in my WPF project to specify a maximum length of strings, with the following: using System.ComponentModel.DataAnnotations; However, I get the error The type or namespace name 'DataAnnotations' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference?) I've seen other examples where DataAnnotations does exist in this namespace. I'm using C#4. Is there any reason why I can't use this? What can I do to fix it? Hari You have to reference the assembly in which this namespace is defined (it is not referenced by default in the

How to use DataAnnotations ErrorMessageResourceName with custom Resource Solution

时光总嘲笑我的痴心妄想 提交于 2019-11-27 03:59:43
问题 I'm building a MVC web application with C#. Since the site will be multilingual, I've implemented my own ResourceManager. This class is responsible for fetching the required resource strings from a database/cache depending on the currents thread culture and works fine so far. My problem is, I'd like to use the my custom ResourceManager solution to fetch validation error messages when for example using the Required Attribute on a property. Can this be done? 回答1: The RequiredAttribute allows to

DataAnnotations: Recursively validating an entire object graph

大城市里の小女人 提交于 2019-11-27 03:55:14
I have an object graph sprinkled with DataAnnotation attributes, where some properties of objects are classes which themselves have validation attributes, and so on. In the following scenario: public class Employee { [Required] public string Name { get; set; } [Required] public Address Address { get; set; } } public class Address { [Required] public string Line1 { get; set; } public string Line2 { get; set; } [Required] public string Town { get; set; } [Required] public string PostalCode { get; set; } } If I try to validate an Employee 's Address with no value for PostalCode , then I would

Where are the Entity Framework t4 templates for Data Annotations?

白昼怎懂夜的黑 提交于 2019-11-27 03:48:56
问题 I have been googling this non stop for 2 days now and can't find a single complete, ready to use, fully implemented t4 template that generates DataAnnotations. Do they even exist? I generate POCOs with the standard t4 templates. The actual database table has metadata that describes some of the validation rules, eg not null, nvarchar(25), etc. So all I want is a t4 template that can take my table and generate a POCO with DataAnnotations, eg public class Person { [Required] [StringLength(255)]

Extending the MVC RequiredAttribute

久未见 提交于 2019-11-27 03:27:40
问题 I have an extended class of RequiredAttribute that doesn't send error messages back. If I check it in the debugger the text is there alright. public class VierRequired : RequiredAttribute { public VierRequired(string controlName) { //... } public string VierErrorMessage { get { return ErrorMessage; } set { ErrorMessage = value; } } // validate true if there is any data at all in the object public override bool IsValid(object value) { if (value != null && !string.IsNullOrEmpty(value.ToString()

MVC Razor Validation Errors showing on page load when no data has been posted

对着背影说爱祢 提交于 2019-11-27 03:11:39
问题 I'm messing around with data annotations. When I click on a link to go to a page, the validation messages are being displayed, but I would like to have the validation messages not show unless data has been posted. View: @Html.TextBoxFor(m => m.EmailAddress, new { @placeholder = "Enter Email", @class = "form-control" }) @Html.ValidationSummary(true, "Registration Failed. Check your credentials") @Html.ValidationMessageFor(m => m.EmailAddress, "You must enter a valid Email Address.") Model: