data-annotations

System.ComponentModel.DataAnnotations.Schema not found

家住魔仙堡 提交于 2019-12-05 01:17:19
I am running into an issue in Visual Studio 2012 that involves the System.ComponentModel.DataAnnotations.Schema namespace. It tells me that the ForeignKeyAttribute cannot be resolved, the solution in the past was to add the using statement that is commented out below. VS2012 can't resolve the Schema namespace as VS2010 was able to. Has anything changed in recent .Net releases that would be causing this problem? If so, how do I work around them? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; // using System

Mark a field “Read Only” with Data Annotations

◇◆丶佛笑我妖孽 提交于 2019-12-05 00:36:51
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(); } } } vijay Mark Property with ReadOnly attribute. [ReadOnly(true)] public decimal BodyMassIndex { get; private set;

How Can I Use Custom Validation Attributes on Child Models of a DB Entity?

丶灬走出姿态 提交于 2019-12-05 00:15:35
Summary: I want a data annotation validator to reference another property in the same class ( TitleAuthorAndPublishingConfiguration ). However, DB.SaveChanges() is not being called on this class directly. Rather it is being called on the parent of this class ( WebsiteConfiguration ). Therefore validationContext.ObjectType is returning WebsiteConfiguration and I am unable to refer to properties of TitleAuthorAndPublishingConfiguration within the data annotation validator. WebsiteConfiguration.cs public class WebsiteConfiguration { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

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

强颜欢笑 提交于 2019-12-04 23:56:50
问题 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"

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

烂漫一生 提交于 2019-12-04 23:52:52
问题 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

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

此生再无相见时 提交于 2019-12-04 23:24:22
问题 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

ASP.NET MVC ValidateInput(false) stops working with xVal and [RegularExpression] DataAnnotation

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 22:31:31
I would like to intercept the "<" character in the form field by a regex validator. I will describe the problem in 3 steps: Step 1 : When I try to submit a form with a field containing the "<" character, I get the "Potentially dangerous request..." - as expected in ASP.NET. Step 2 : To avoid ASP.NET's RequestValidation, I decorate my Update method in the controller with "[ValidateInput(false)]". It works as expected - now I can post "<" character without error. Step 3 : I use xVal with DataAnnotations. For example, [Required] or [StringLength(255)] works as expected. BUT when I use:

Validation on int data type asp .net mvc3

可紊 提交于 2019-12-04 19:46:47
I am getting The value 'abc' is not valid for fieldName. as error message. which is default error message and i want to override it in easier way. as of now what i have tried is listed below [RegularExpression(@"^\d+$",ErrorMessage="enter numeric value")] [Integer(ErrorMessageResourceType = typeof(appName.Resources.abc.Resource), ErrorMessageResourceName = "error_numeric")] [RegularExpression("([1-9][0-9]*)")] Range(1,int.max,ErrorMessage="enter numeric value") but failed to change default error message. Suggest me the easiest possible way to do this. using System; using System.Collections

MVC3 validation with data-annotation?

别说谁变了你拦得住时间么 提交于 2019-12-04 19:23:21
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. Something like: [Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")] public int Value { get; set; } Making a drop-down list

How to write custom RegularExpressionValidator which takes the values from the config file?

坚强是说给别人听的谎言 提交于 2019-12-04 18:59:09
问题 I have to use a Regular expression validator for username property in my model. I am getting this regular expression from the config file. [RegularExpression(UsernameValidationExpression)] //UsernameValidationExpression = value from the config file public string UserName { get; set; } Here i am getting an error "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type" How can i solve this?? 回答1: public class