data-annotations

Entity Framework 6 Reusing Data Annotations

不打扰是莪最后的温柔 提交于 2019-12-05 16:25:12
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"), Required(), DataType(DataType.Password), StringLength(255, MinimumLength = 7, ErrorMessage = "The

So is an [Email] attribute built in ASP.NET MVC 3 or not?

…衆ロ難τιáo~ 提交于 2019-12-05 15:48:29
问题 The [Email] attribute was going to be built into ASP.NET MVC 3 as it was in futures? So is it now available or not? I guess it is quite a dumb question but I've spent some time googling and didn't find any normal answer. The email regex: "^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]

RemoteAttribute validator does not fire server-side

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 14:52:03
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 omission - I think most people would assume that the RemoteAttribute would work like all the other built-in

WebApi POST not to include ID field

删除回忆录丶 提交于 2019-12-05 14:42:26
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: public HttpResponseMessage Post(MyModel model) When I start the project and go to auto-generated API

MVC3 Localized display name attribute by property name

旧巷老猫 提交于 2019-12-05 13:54:34
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 { get; set; } [Required] [StringLength(40)] [LocalizedDisplayNameAttribute("User_LastName")] public string

MVC Dataannotation validation rule for a collection?

和自甴很熟 提交于 2019-12-05 13:34:31
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. I think something like this might help: public class MinimumCollectionSizeAttribute : ValidationAttribute { private int _minSize; public MinimumCollectionSizeAttribute(int

Why use primary keys?

我的未来我决定 提交于 2019-12-05 13:24:24
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? Mostly for referential integrity with foreign keys,, When you have a PK it will also create an index behind the scenes and this way you don't need table scans when looking up values RDBMS providers are usually optimized to work with tables

ASP.NET MVC 3 Data Annotation: Add validation dynamically

落花浮王杯 提交于 2019-12-05 13:03:59
问题 I'm new with data annotation. I'd like to know if it possible (and how) to add some validation dynamically. It is very extensive to explain why, but I've a ViewModel that receives and object when created. In that object I must check for some property and depending its value I should have or not some validations. An example: public class ProfileViewModel { [Required(ErrorMessage = "The field {0} is required")] [Display(Name = "Client Code")] public int ClientCode { get; set; } [Required

Read DataAnnotations from a collection of models in an MCV2 view

笑着哭i 提交于 2019-12-05 12:30:40
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 loop I can write <%: Html.LabelFor(c => item.Name) %> but is there any way to access this information

ModelState.IsValid vs IValidateableObject in MVC3

好久不见. 提交于 2019-12-05 11:46:03
问题 so according to Gu IValidatableObject.Validate() should get called when a controller validates it's model (i.e. before ModelState.IsValid ) however simply making the model implement IValidatableObject doesn't seem to work, because Validate(..) doesn't get called. Anyone know if there is something else I have to wire up to get this to work? EDIT: Here is the code as requested. public class LoginModel : IValidatableObject { [Required] [Description("Email Address")] public string Email { get;