data-annotations

Data Annotation Ranges of UK Datetime fields

久未见 提交于 2019-12-23 18:21:13
问题 I want to use Data Annotations to validate DateTime fields, but I'm running into problems. According to documentation on MSDN (http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute.aspx), the following should do the job [Range(typeof(DateTime), "1/2/2004", "3/4/2004", ErrorMessage = "Value for {0} must be between {1} and {2}")] However, this marks any date I enter as invalid! At first I thought it was not picking up UK dates (when I tried 26/2/2004) but

Second Self-To-Self relationship in Entity Framework

故事扮演 提交于 2019-12-23 12:59:37
问题 Assume we have a domain class public class Incident { [Key] public virtual int IncidentId { get; set; } [Display(Name = "Parent Incident")] public virtual Incident ParentIncident { get; set; } [Display(Name = "Related Claim")] public virtual Incident ClaimIncident { get; set; } } Other properties are omitted for simplicity. When I had just ParentIncident in place, everything worked fine. Now I have added ClaimIncident to the class. I am attempting to update my database using the Entity

Is there a way through data annotations to verify that one date property is greater than or equal to another date property?

醉酒当歌 提交于 2019-12-23 10:10:32
问题 I have a StartDate and EndDate on my SchoolEvents Model and I was wondering if there are any data annotations I could use to verify that the StartDate is less than or equal to the EndDate and that the EndDate is greater than or equal to the StartDate ? 回答1: From my point of view, you have to build a custom validation attribute. You can look at the link to validate follow specific your validation. It will take your efforts so much. Instead of you use data annotation you should apply Fluent

ASPNET MVC - Override Html.TextBoxFor(model.property) with a new helper with same signature?

断了今生、忘了曾经 提交于 2019-12-23 08:56:09
问题 I want to override Html.TextBoxFor() with my own helper that has the exact same signature (but a different namespace of course) - is this possible, and if so, how? The reason for this is that I have 100+ views in an already existing app, and I want to change the behaviour of TextBoxFor so that it outputs a maxLength=n attribute if the property has a [StringLength(n)] annotation. The code for automatically outputting maxlength=n is in this question: maxlength attribute of a text box from the

MVC5 - Data Annotations - Client Side Validation Not Happening?

有些话、适合烂在心里 提交于 2019-12-23 07:55:41
问题 I have an MVC 5 app, and I'm using data annotations to do a majority of validation. One of the properties in my class looks like this: [Required(ErrorMessage = "Please enter a business name")] [StringLength(80)] public string BusinessName { get; set; } The validation is working but it doesn't appear to be happening in the browser like I thought it should. On my page I have a Save button. If I leave the Business Name field blank and click Save, a post is done to a controller method that looks,

Extract Data Annotations in custom ModelBinder

人走茶凉 提交于 2019-12-23 03:42:20
问题 I’m using a custom model binder in MVC that implements System.Web.Mvc.IModelBinder. The model binder takes a generic type (class) extracts each of the class properties and stores these in a List along with additional details about each property. For example for each Property it stores access permissions i.e. Read, Write, None for each property based on the logged in user. Then in my View I use this additional data to determine whether to display a specific property or not. I want to be able

DataTypeAttribute is showing the wrong message

懵懂的女人 提交于 2019-12-23 01:28:33
问题 I create the following attribute: public class SpecificDataTypeAttribute : DataTypeAttribute { public SpecificDataType(DataType dataType, string field) : base(dataType) { this.ErrorMessage = string.Format("{0} {1}", field, Messages.SpecificDataTypeAttribute); } } And use like: [SpecificDataType(DataType.DateTime, "Initial date")] public DateTime? InitialDate { get; set; } So, the message that is in Messages.SpecificDataTypeAttribute is "is in a incorrect format." . When i input a wrong date

asp.net mvc int property bind exception

牧云@^-^@ 提交于 2019-12-22 18:47:35
问题 I have a int property in my class and want to validate if the user has entered a string. How can I do that using data annotations? When I pass a non-integer value I get a excpetion like this: The value 'asdasd' is not valid for property. For example using this validation attribute: [Range(0, Int32.MaxValue, ErrorMessage="Invalid Number")] public int? Number { get; set; } and entering 'aaa' in a field tha uses the model I've got an excepetion with this message: The value 'aaa' is not valid for

Using html in ErrorMessage property of data annotations

时光总嘲笑我的痴心妄想 提交于 2019-12-22 11:09:08
问题 Anyone know if it is possible to do the following: public class User { public Guid UserID { get; set; } [Required(ErrorMessage="A school selection is required.")] [Range(1, int.MaxValue, ErrorMessage="School not found. Please contact support at <a href='mailto:support@mysite.com'>support@mysite.com</a>")] public int SchoolID { get; set; } // ... other properties } And not have @Html.ValidationMessageFor(model => model.SchoolID) encode the HTML? Maybe I need to make a custom helper extension

What's DisplayAttribute.GroupName property used for?

我们两清 提交于 2019-12-22 11:09:04
问题 I'm trying to figure out the valid usages of DisplayAttribute.GroupName property. MSDN says: A value that is used to group fields in the UI. but I wouldn't call it a comprehensive explanation. It makes me think that GroupName can be used to create groupboxes around certain fields. But then the remark: Do not use this property to get the value of the GroupName property. Use the GetDescription method instead. A null value or empty string is valid. seems to contradict it. So what is this