data-annotations

How to get DataAnnotations working in asp.net 4.5 WebForms?

一世执手 提交于 2019-12-22 09:31:02
问题 I'm using .net 4.5 WebForms with Model Binding and Entity Framework 5. Part of my webform: <asp:ListView ID="MenteeModuleList" ItemPlaceholderID="itemPlaceHolder" OnCallingDataMethods="MenteeModuleList_CallingDataMethods" ItemType="PFA_Mentorship.BLL.MenteeModuleBL+MenteeModuleGrid" DataKeyNames="MenteeModuleID" SelectMethod="GetMenteeModulesGrid" InsertItemPosition="LastItem" InsertMethod="InsertMenteeModule" runat="server"> <LayoutTemplate> <table runat="server"> <tr runat="server"> <th id=

MVC3 Localized display name attribute by property name

余生颓废 提交于 2019-12-22 08:40:16
问题 I have recently learned how to create a localized display names for my model's properties using the following article: Simplified localization for DataAnnotations And I am now trying to push it a bit further by removing the parameters from the constructor. Meaning instead of having this public class User { [Required] [LocalizedDisplayNameAttribute("User_Id")] public int Id { get; set; } [Required] [StringLength(40)] [LocalizedDisplayNameAttribute("User_FirstName")] public string FirstName {

Read DataAnnotations from a collection of models in an MCV2 view

纵饮孤独 提交于 2019-12-22 06:56:57
问题 In my MVC2 AdminArea I'd like to create an overview table for each of my domain models. I am using DataAnnotations like the following for the properties of those domain model objects: [DisplayName("MyPropertyName")] public string Name { get; set; } Now my question is: How can I access the DisplayName Attribute if my view receives a collection of my domain models? I need this to build the table headers which are defined outside of the usual <% foreach (var item in Model) { %> loop. Inside this

Data annotations on WCF service contracts

南楼画角 提交于 2019-12-22 05:42:45
问题 I have a WCF service that has a [DataContract] class defined in it. Each of the properties has the [DataMember] attribute and I have added a couple of Data Annotation attributes [Required] and [StringLength] to a couple of the properties. I then consume this service in an asp.net MVC application as a service reference. When I get a list of all the attributes using var attr= from prop in TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>() from attribute in prop.Attributes.OfType

Dataannotations localization

时间秒杀一切 提交于 2019-12-22 05:33:21
问题 model [MetadataType(typeof(UserMetaData))] public class User { public int Id { get; set; } public string UserName { get; set; } } public class UserMetaData { public int Id { get; set; } [Required(ErrorMessageResourceType = typeof(Resources.ModelValidation), ErrorMessageResourceName = "UserNameRequired")] [LocalizedDisplayNameAttribute("UserName", NameResourceType = typeof(Resources.ModelValidation))] public string UserName { get; set; } } view @using (Html.BeginForm()) { <div> @Html.LabelFor

Add Data Annotation To Entity Framework(Or Linq to SQL) generated class

女生的网名这么多〃 提交于 2019-12-22 05:06:54
问题 Is it possible to add more Data Anootation member such as Range , Required ,... to Entity Framework or Linq to SQL generated classes automatically ? I want to use data annotation validation for my classes thanks Important:Related to this topic: using Metadata with Entity Framework to validate using Data Annotation EDIT 1) I create an entity framework model for Northwind database and add Product class.a part of code is like this: [EdmEntityTypeAttribute(NamespaceName="NorthwindModel", Name=

ASP.NET MVC4: An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type

空扰寡人 提交于 2019-12-22 04:37:11
问题 I had the following code: [Required(ErrorMessage = MessageModel.translateMessage("required")))] [Display(Name= MessageModel.translateMessage("id"))] public string user_id { get; set; } I am trying to make the error message dynamic but I get error when compiled.: "An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type." Any solution for this issue? 回答1: First you create a Resource .resx file this will contain your

MVC 4 razor Data Annotations ReadOnly

戏子无情 提交于 2019-12-22 04:28:10
问题 The ReadOnly attribute does not seem to be in MVC 4. The Editable(false) attribute does not work the way I would want it to. Is there something similar that works? If not then how can I make my own ReadOnly attribute that would work like this: public class aModel { [ReadOnly(true)] or just [ReadOnly] string aProperty {get; set;} } so I can put this: @Html.TextBoxFor(x=> x.aProperty) instead of this ( which does work ): @Html.TextBoxFor(x=> x.aProperty , new { @readonly="readonly"}) or this (

Mark a field “Read Only” with Data Annotations

南楼画角 提交于 2019-12-22 03:47:04
问题 I am trying to make the ID field read only. It is an Identity field in the DB so the user will not be setting it. However they would like to see it. What am I missing as the below, when assigned to a DataForm still allows that value to be Edited. public class StatusChoice : BindableBase { private int id; [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Editable(false,AllowInitialValue = false)] public int ID { get { return id; } set { id = value; OnPropertyChanged(); } } } 回答1:

MVC3 validation with data-annotation?

只愿长相守 提交于 2019-12-21 23:24:14
问题 Is it possible to use a data-annotation attribute for dropdownlists to be valid, only if the user selects an option different from one with the value o (zero). The option with value o (zero) is "Please Select an account", which is selected by default. I used [Required] attribute to validate this dropdownlist, but it doesn't have any effect, because, how I said, by default the option with value o (zero)- "Please Select an account"- is selected. 回答1: Something like: [Range(1, int.MaxValue,