data-annotations

Data annotation and wpf validation

两盒软妹~` 提交于 2019-12-12 05:24:58
问题 Is there any way that I use data annotation as the source of validation in WPF? I want to be able to define a class such as: class myData { [Required] [MaxLength(50)] public string Name{get;set;} } And then bind it to a field in a view and the wpf validate that user enter some value for this field and also make sure that its length is not greater than 50. I know that I can write a validator for this, but then if I change the maxLength to say 60, then I need to change it in validator and I don

Why this ASP.NET Core DataAnnotation works in ViewModel but not in Model alone

隐身守侯 提交于 2019-12-12 04:57:10
问题 In my one ASP.NET Core app, a View is using following ViewModel. The underlying model of the ViewModel has an attribute StartDate . The model has DataAnnotion on StartDate but I'm not using the same DataAnnotation on the StartDate in ViewModel since I thought if DataAnnotation is in your Model then it gets aggregated to your viewmodels . And hence, the following View should display the StartDate as Date only. But the View is displaying StartDate as date and time, e.g. 9/30/2015 12:00:00 AM .

Putting DataAnnotation buddy class in another assembly

流过昼夜 提交于 2019-12-12 02:28:09
问题 I have a project called Model . All of my entities are in that project generated by EF Code first. Public Partial Class Person { ... } I don't want to touch my generated classes so I can create a partial class and add MetadataType attribute to it. [MetadataType(typeof(Person_Metadata))] public partial class Person { } And here is my buddy class . [Bind(Exclude="PersonID")] public class Person_Metadata { [Display(Name:="First name")] public string FirstName { get; set; } [Display(Name:="Last

Entity Framework CTP 5 RelatedTo Attribute not working

﹥>﹥吖頭↗ 提交于 2019-12-12 01:48:22
问题 Using Entity Framework CTP 5 I am trying to make a list of foreign keys in my class. But I keep getting an error which says RelatedTo can not be found. Here is the code: public class VertragsVerweis : GenericBLL { [Key] public String Uid { get; set; } public String VertagsVerweisNr { get; set; } public String Bezeichnung { get; set; } public Boolean Reparatur { get; set; } [RelatedTo(RelatedProperty="Artikel")] public List<Artikel> Artikelen { get; set; } } This gives me the error: Error 2

Model Validation not working with all properties

醉酒当歌 提交于 2019-12-12 01:47:24
问题 I have the following ViewModel: public class MyViewModel:IValidatableObject { public int Id { get; set; } public string Name { get; set; } public DateTime? Birthday { get; set; } public int Status { get; set; } public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { if (string.IsNullOrWhiteSpace(Name)) yield return new ValidationResult("Please fill the name", new string[] { "Name" }); if (Birthday.HasValue == false) yield return new ValidationResult("Please fill

setting displayname data annotation dynamically in asp.net mvc

心不动则不痛 提交于 2019-12-11 23:17:32
问题 I have a database table with the following fields item_key, item_value, display_name, uihint I want to be able to specify in the database table which displaytemplate to use and also the displayname. <%= Html.EditorFor(p=>pageField.item_value, pageField.uihint) %> The UIHint is working, but I can't work out a way of setting the displayname dynamically, the above code is producing something like item_value TEXTBOX item_value TEXTBOX item_value TEXTBOX Where I want the item_values titles to be

ASP.NET MVC DisplayAttribute and interfaces

这一生的挚爱 提交于 2019-12-11 21:00:58
问题 I have some interface and classes public inteface IRole { int Id { get; } string Name { get; set; } } public class Role : IRole { public int Id { get; } [Display("Role Name")] public string Name { get; set; } } public class Member { [Display("Login")] public string Login { get; set; } [Display("Password")] public string Password { get; set; } public IRole Role { get; set; } } on View I try to use, View is strongly type of Member on this line displays correct message from DisplayAttribute <%=

How to create custom validation attribute?

允我心安 提交于 2019-12-11 20:56:59
问题 I am trying to create a custom validation attribute. public class PhoneValidator : ValidationAttribute { public override bool IsValid(object value) { return new RegularExpressionAttribute(@"^[+0]\d+").IsValid(Convert.ToString(value).Trim()); } } And I use this to use [PhoneValidator] public string PhoneNumber { get; private set; } I copied it from a website, in theory this should work. But I couldn't make it work. 回答1: Here is example from MSDN: PhoneMaskAttribute .cs: using System; using

Trying to modify a builder to style text in DataAnnotation Attribute [Display(Name=“Text”)]

半世苍凉 提交于 2019-12-11 19:47:01
问题 I asked a question and got a great answer for my specific problem, but have run into a problem with <select> lists using <label> and trying to style the DataAnnotation Attribute [Display(Name="Text")] with something like [Display(Name="Text <span class=\"myclass\">Special styled text</span>.")] . Here is the Question/Answer link. The answer explained about Html Encoding and presented me with a solution for the Html.LabelFor helper. It worked perfectly for my immediate situation. However, I

mapping classes for 1:* relationship in Entityframework - ASP.NET MVC

浪子不回头ぞ 提交于 2019-12-11 18:14:47
问题 i am working on asp.net mvc app and using data annotation to map database table in entity framework classes. Now i have two table say example table-X and table-Y with many-to-many relationship so introduce another table in between 'table-XY' to sort to one-many relationship.... how i am going to map table-XY? do i put iCollectiontable-XY in table-X and same for table-Y?? i need some guidance for mapping! table-X [Table("table-X")] public class table-X { public table-X() { } [Key] public int