data-annotations

DataAnnotations Support for WinForms

折月煮酒 提交于 2020-02-15 08:16:53
问题 I would like to use data annotations on my classes and entities in a Windows Forms application. I use windows DataGridViews and Infragistics UltraGrids. I have previously successfully used the [DisplayName("Name to Display")] attribute to set the column header text on a DataGridView/UltraGrid. This is very beneficial, because I may have several grids displaying this class, and instead of configuring each grid to display the appropriate header text, I can simply set one data annotation. I'd

Applying Data Annotations to sub properties of the View Model in MVC?

。_饼干妹妹 提交于 2020-02-01 03:26:17
问题 Putting simple Data Annotations on properties is great, public class UnicornViewModel { [Required] public string Name { get; set; } But lets say I'm have something like this: public class SuperPower { public class Name { get; set; } } public class UnicornViewModel { [Required] public string Name { get; set; } public SuperPower PrimarySuperPower { get; set; } public SuperPower SecondarySuperPower { get; set; } How do I apply the Required attribute on PrimarySuperPower.Name while leaving it

How to define matching column in InverseProperty

守給你的承諾、 提交于 2020-01-17 08:11:09
问题 In my MVC application, I have an entity relation between Student and Lookup tables with StatusID-LookupKey and GenderID-LookupKey pairs inestead of (StatusID-ID and GenderID-ID pairs). However, not being able to define the LookupKey column as the matching parameter with the StatusID and GenderID, I have to use ID column of Lookup table. How can I use InverseProperty and match the related columns to the LookupKey column? Thanks in advance. Here are the Lookup table and the entities below:

EF - Require either of two fields?

旧巷老猫 提交于 2020-01-16 04:20:06
问题 Is there a way to do what this question is doing (Either Or Required Validation) but instead of applying it on the entire class, I only want to apply the validation on two specific fields within the class. For example public class FooModel { [Required] public string Name { get; set; } [Required] public decimal Cost { get; set; } public int Bar1 { get; set; } public float Bar2 { get; set; } } I only want to constrain Bar1 and Bar2 to be an either/or requirement. They cannot both be null. Is

MVC3 - Get Data Attribute in html.helper

早过忘川 提交于 2020-01-15 02:42:29
问题 I'm trying to create a custom attribute to control formatting in a custom HTML helper object. The source code for my custom selector class is (Code is from http://forums.asp.net/t/1649193.aspx/1/10). public static MvcHtmlString DdUovFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes) { ModelMetadata metadata = ModelMetadata

MVC3 - Get Data Attribute in html.helper

最后都变了- 提交于 2020-01-15 02:42:27
问题 I'm trying to create a custom attribute to control formatting in a custom HTML helper object. The source code for my custom selector class is (Code is from http://forums.asp.net/t/1649193.aspx/1/10). public static MvcHtmlString DdUovFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes) { ModelMetadata metadata = ModelMetadata

ASP.NET MVC: How to execute Data Annotation validations in the service layer?

╄→гoц情女王★ 提交于 2020-01-14 10:34:15
问题 In a recent question posed here: ASP.NET MVC: Is Data Annotation Validation Enough? ...it was concluded that relying on data annotation validation (triggered by the model binder) was not enough to ensure that validation was always executed. We still need to add the same validation logic in the services layer (or somewhere else after ModelBinding happens). The unfortunately thing about this is that we will be duplicating our validation code (once with Data Annotations and again in the services

MVC 3 DataAnnotations: Not allow HTML

不打扰是莪最后的温柔 提交于 2020-01-13 18:29:28
问题 Is there anyway to use DataAnnotations in MVC 3 to not allow HTML is used in a textbox? I see a way to allow using HTML (AllowHTMLAttribute) but what if i dont want the user to type any HTML in the textbox and want to warning him? Thanks :) 回答1: You have to write a custom RegularExpressionAttribute ... something like this: public class DisallowHTMLAttribute : RegularExpressionAttribute { public DisallowHTMLAttribute() : base(@"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>"

Custom validation error message if user puts a non-numeric string in an int field

房东的猫 提交于 2020-01-13 08:32:29
问题 This question has to have been asked before, but I think the search terms are too generic for me to find the answer I'm looking for, so I'll ask it again. I have a model with an int property, and a range annotation. If the user enters something other than an int, the validation message responds with The value '<bad data>' is not valid for '<property name>' ... which is great, but I want to provide a bit more feedback, i.e., Expecting an integer value in this field. . Since this validation

data annotations hide property/field

旧街凉风 提交于 2020-01-13 02:47:09
问题 I have a model class Address { public int AddressID {get;set;} public string Street {get;set;} public string City {get;set;} public string State {get;set;} public int ZipCode {get;set;} } in my view, when I have @Html.LabelFor(model => model.Address) (assuming Address is a complex property inside another model) I get a label for every one of Address properties, so I get: AddressID: Street: City: State: ZipCode: problem is, I don't want the ID property to show up, I tried these two annotations