data-annotations

asp.net 4.5 webforms model binding : client side validation supported?

旧城冷巷雨未停 提交于 2019-12-03 16:44:02
I'm a huge fan of asp.net 4.5 webforms model binding using data annotations. ascx: <asp:FormView ItemType="Contact" runat="server" DefaultMode="Edit" SelectMethod="GetContact" UpdateMethod="SaveContact"> <EditItemTemplate> <asp:ValidationSummary runat="server" ID="valSum" /> Firstname: <asp:TextBox runat="server" ID="txtFirstname" Text='<%#: BindItem.Firstname %>' /> Lastname: <asp:TextBox runat="server" ID="txtLastname" Text='<%#: BindItem.Lastname %>' /> Email: <asp:TextBox runat="server" ID="txtEmail" Text='<%#: BindItem.Email %>' /> <asp:Button ID="Button1" runat="server" Text="Save"

Is it possible to use a variable for the `[Display(Name=“Something”)]` data annotation in MVC3 (C#)

孤街醉人 提交于 2019-12-03 16:42:09
not sure why, but the data annotation in MVC3 insist on having constant values, which i just can't understand for things like error messages and display names. I love these annotations, they are so easy to use and so powerful, but what if you need to support multiple languages? Imagine i have the following model: public class Person { public string First_Name { get; set; } } If i dont change anything and use the CRUD views that MVC3 will build for me I get a label next to the text fields that says "First_Name", so i add the data annotation to change the display name like so: public class

Understanding ValidationContext in DataAnnotations

假如想象 提交于 2019-12-03 16:25:01
问题 I want to utilize Validator.TryValidateValue() but don't understand the mechanics. Say, i have the following: public class User { [Required(AllowEmptyStrings = false)] [StringLength(6)] public string Name { get; set; } } and the method: public void CreateUser(string name) {...} My validation code is: ValidationAttribute[] attrs = bit of reflection here to populate from User class var ctx = new ValidationContext(name, null, null); var errors = new List<ValidationResult>(); bool valid =

“Display Name” Data Annotation for class using c#

荒凉一梦 提交于 2019-12-03 15:19:56
I have a class with [Display(Name ="name")] set in the properties, and [Table("tableName"] in the top of the class. Now I'm using reflection to get some information of this class and I'm wondering if somehow I can add a [Display(Name ="name")] to the class itself. It will be something like [Table("MyObjectTable")] [Display(Name ="My Class Name")] <-------------- New Annotation public class MyObject { [Required] public int Id { get; set; } [Display(Name="My Property Name")] public string PropertyName{ get; set; } } Based on that article I referenced heres a complete example Declare Custom

DateTime Data Annnotations do not work in ASP.NET Core RC2 application

与世无争的帅哥 提交于 2019-12-03 14:57:54
if I add Data annotations to my ViewModel the value of the DateTime field is always {1/1/0001 12:00:00 AM}. ViewModel public class EditReleaseViewModel : BaseViewModel { [Display(Name = "ReleaseDate", ResourceType = typeof(SharedResource))] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy HH:mm}")] [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(SharedResource))] public DateTime ReleaseDate { get; set; } } The controller is very straightforward, if I remove the data annotation and use the default everything works fine.

Create Foreign Key using Data Annotations

自闭症网瘾萝莉.ら 提交于 2019-12-03 14:46:16
In the code below, I need to set a foreign key constrant on ParentInfoAddProperties.ParentQuestionAnswersId so that it is dependent on ParentQuestionAnswers.Id (which is a Primary Key). I am attempting to do so using data annotations but Entity Framework 6 wants to create a new Foreign Key column in my ParentQuestionAnswers table which references the ParentInfoAddProperties.Id column not the ParentInfoAddProperties.ParentQuestionAnswersId column. I do not want Entity Framework to create a new foreign key column. I'd greatly appreciate if someone can explain what data annotations or (if

How to hide Entity Framework entity properties from strongly typed views?

不羁的心 提交于 2019-12-03 14:39:24
I am using Entity Framework in my ASP.NET MVC 4.0 application and I want to know how to prevent or hide fields from my entity from being generated in my strongly typed view? Right now several primary key fields and timestamp fields are being generated on the view which I do not want. I know setting the property to internal as opposed to public works but I am not sure of the total downstream effect this will have. I prefer to use data annotations on the properties but the ones I have tried prevent Controller scaffolding or make them as hidden fields. I prefer for them to remain public but just

ASP.NET MVC: Adding custom ErrorMessage that incorporates DisplayName to custom ValidationAttribute

断了今生、忘了曾经 提交于 2019-12-03 14:31:46
I am using ASP.NET MVC with DataAnnotations. I have created the following custom ValidationAttribute which works fine. public class StringRangeAttribute : ValidationAttribute { public int MinLength { get; set; } public int MaxLength { get; set; } public StringRangeAttribute(int minLength, int maxLength) { this.MinLength = (minLength < 0) ? 0 : minLength; this.MaxLength = (maxLength < 0) ? 0 : maxLength; } public override bool IsValid(object value) { //null or empty is <em>not</em> invalid string str = (string)value; if (string.IsNullOrEmpty(str)) return true; return (str.Length >= this

How to automatically add placeholder attribute to html input type number in mvc 4?

假如想象 提交于 2019-12-03 12:53:14
问题 This is a very specific issue. I managed to automatically add the placeholder attribute to html5 email input type by using an editor template called EmailAddress.cshtml , saved in ~/Views/Shared/EditorTemplates/ folder. See the code below: @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line", placeholder = ViewData.ModelMetadata.Watermark }) It works because i'm using the [DataType(DataType.EmailAddress)] DataAnnotation in my view model. What

MVC3: How to change the generic [Required] validation message text?

。_饼干妹妹 提交于 2019-12-03 12:45:31
When you decorate a model object's property with the Required attribute and don't specify ErrorMessage or ResourceType/Name you get the validation message in the interpolated form of "The {0} field is required.", where param 0 is the value of the DisplayName attribute of that property. I want to change that default string to something else but I want to keep the generic nature of it, that is I don't want to specify ErrorMessage or ResourceType/Name for every property of the model object. Where is the default string stored and how can I change it? Deriving your own attribute is a fair option