data-annotations

RemoteAttribute validator does not fire server-side

ぐ巨炮叔叔 提交于 2019-12-07 11:38:44
问题 It appears that the RemoteAttribute validator introduced in ASP.NET MVC 3 does not validate on the server-side, only via JavaScript. If you turn off JS in your browser, you will find that on model binding, the validation controller action (that you specified when decorating a model property with the RemoteAttribute) will not be hit. In fact, if you inspect the source code for the RemoteAttribute, you will find the IsValid methods just returns true in all cases. This seems to be quite an

WebApi POST not to include ID field

耗尽温柔 提交于 2019-12-07 11:37:20
问题 I am still just a couple days into ASP.NET and WebAPI frameworks so I must be missing out something really simple. I have a model that has a couple properties and ID (as a property, which has a private setter but that didn't help). public long ID { get; private set; } [Required(ErrorMessage = "Location coordinate X is required.")] public double X { get; set; } [Required(ErrorMessage = "Location coordinate Y is required.")] public double Y { get; set; } And then I have a controller method post

Why use primary keys?

落花浮王杯 提交于 2019-12-07 10:45:10
问题 What are primary keys used aside from identifying a unique column in a table? Couldn't this be done by simply using an autoincrement constraint on a column? I understand that PK and FK are used to relate different tables, but can't this be done by just using join? Basically what is the database doing to improve performance when joining using primary keys? 回答1: Mostly for referential integrity with foreign keys,, When you have a PK it will also create an index behind the scenes and this way

Using Foolproof RequiredIf with an Enum

删除回忆录丶 提交于 2019-12-07 10:08:33
问题 We are trying to use the Foolproof validation annotation [RequiredIf] to check if an email address is needed. We also created an enum to avoid using a lookup table id in the ViewModel. The code looks like this: public enum NotificationMethods { Email = 1, Fax = 2 } Then in the ViewModel: [RequiredIf("NotificationMethodID", NotificationMethods.Email)] public string email {get; set;} In this senario we do not get an error when email is unfilled but selected as the notification type. Conversely

One out of 2 properties should be null (EntityFramework Code First)

扶醉桌前 提交于 2019-12-07 09:55:30
问题 I've searched a lot in the forum, but haven't found anything about regarding this issue. I have 2 properties in the EntityFramework Code First: [Column(TypeName = "Money")] public decimal? Debit { get; set; } [Column(TypeName = "Money")] public decimal? Credit { get; set; } One of them should be not null, but the other one should be null Examples: Debit=null; Credit=34; Debit=45; Credit=null; On the other hand, it should not be possible to set both or none of them null. Is it possible to

ASP.NET MVC - Pass model value to data annotation parameter

南楼画角 提交于 2019-12-07 08:47:30
问题 I want to pass a value from one of my properties in my model to my data annotation to validate my password property, but I have no idea how I can achieve this. When I am doing this at this way I get the following error: an attribute argument must be a constant expression typeof expression or array My model: public class LoginModel { public string Voornaam { get; set; } public string Achternaam { get; set; } public string Gebruikersnaam { get; set; } [Password(AttributeVoornaam = this.Voornaam

MVC Dataannotation validation rule for a collection?

拈花ヽ惹草 提交于 2019-12-07 08:13:53
问题 Is there a dataannotation validate rule for a collection based property? I have the following <DisplayName("Category")> <Range(1, Integer.MaxValue, ErrorMessage:="Please select a category")> Property CategoryId As Integer <DisplayName("Technical Services")> Property TechnicalServices As List(Of Integer) I'm looking for a validator that i can add to the TechnicalServices property to set a minimum for the collection size. 回答1: I think something like this might help: public class

Entity Framework 6 Reusing Data Annotations

那年仲夏 提交于 2019-12-07 07:51:18
问题 I've looked for a while for a definitive solution to this but have yet to come to a conclusion. I would like to specify data annotations just once on the data model class and have these be seen by the UI from a view model class without specifying them again. To illustrate my point suppose I have a UserAccount Class as such... public class UserAccount { [Display(Name = "Username", Prompt = "Login As"), Required()] string UserName { get; set; } [Display(Name = "Password", Prompt = "Password"),

Is it possible to inherit data annotations in C#?

痴心易碎 提交于 2019-12-07 05:26:53
问题 Can I inherit the "password" data annotation in another class? public class AccountCredentials : AccountEmail { [Required(ErrorMessage = "xxx.")] [StringLength(30, MinimumLength = 6, ErrorMessage = "xxx")] public string password { get; set; } } The other class: public class PasswordReset : AccountCredentials { [Required] public string resetToken { get; set; } **["use the same password annotations here"]** public string newPassword { get; set; } } I have to use different models due to API call

Combining DataAnnotations and IDataErrorInfo for WPF

社会主义新天地 提交于 2019-12-07 05:23:53
问题 I am writing a WPF application and I want to use Data Annotations to specify things like Required Fields, Range , etc. My ViewModel classes use the regular INotifyPropertyChanged interface and I can validate the entire object easily enough using the C# 4 Validator , but I would also like the fields to highlight red if they do not validate properly. I found this blog post here (http://blogs.microsoft.co.il/blogs/tomershamam/archive/2010/10/28/wpf-data-validation-using-net-data-annotations-part