data-annotations

Submit MVC3 form that uses Data Annotations with div/jquery

瘦欲@ 提交于 2019-12-11 08:35:24
问题 So, I have a form on the page that I was using an input type="submit" to register users. After adding Data Annotations, some custom, some built-in, I wanted to customize the look of the input submit button only to find out that I cannot get it to match the other buttons on the page which are just divs/links. So, I would like to make it a div that uses jquery to submit the form and call of the data annotation's built-in and customized jquery before submitting. I tried this: @using (Html

How to use ValidationAttribute for user input in options pages?

泪湿孤枕 提交于 2019-12-11 08:34:23
问题 I'm writing a Visual Studio extension. I have a C# class representing an options page. An option is represented by a public property, like this: public class OptionsPageGeneral : DialogPage { [Range(1, 100)] [Category("Misc")] [DisplayName("Integer option")] public string IntOption { get; set; } ... } I'm trying to use the RangeAttribute validation attribute to restrict user input. However, for the given code, user still can input any value, not only from [1; 100] range. I've seen lots

asp.net data annotations field length

时间秒杀一切 提交于 2019-12-11 06:31:01
问题 I currently have the following data annotation on a field [StringLength(1000, MinimumLength = 6, ErrorMessage = "field must be atleast 6 characters")] public string myField {get;set;} Can I change the data annotation such that it works only if the user types something in the field? In other words, it is okay to leave the field empty but if the user types a value in the field, it should be between 6-1000 characters in length. 回答1: That's already the case with the StringLength attribute. If you

Jquery Validation Don't work on Submit if Submit has some Code

不想你离开。 提交于 2019-12-11 06:30:52
问题 I am using MVC Data Anotation with jquery validation for validating my form. It works perfectly on submit but if I write some code on submit click, it doesn't validate it. Why so? Where am I wrong? I am using jquery.validate.unobtrusive.min.js and jquery.validate.min.js I am not getting any kind of error. Just in my case, jquery validation stop working on submit if I put some code on submit click. HTML: <form action="/Login/ChangePassword" id="changePasswordForm" method="post"> <div id=

Entity Framework Database First with proper use of Data Anotations

时光怂恿深爱的人放手 提交于 2019-12-11 06:19:45
问题 I have one project with EF and Code First approach and there using of Data Annotations was straight forward. Now I'm working with Database First and I see that using Data Annotations is more specific so I want to know the right steps to implement it. The structure of my project that provides Data Access is this: In ModelExtensions are all my files that I've created to add the Data Annotations to the DbContextModel.tt entities. Here is the structure of one of my files in ModelExtensions :

Web API 2 XML Serialization is ignoring MetadataType

南楼画角 提交于 2019-12-11 06:18:09
问题 I have a simple class that is generated by Entity Framework 5 , similar to: public partial class Car { public string Model {get; set;} // other properties follow... } I have created a companion class (to keep these things from being overwritten) and a "buddy class" to hold the metadata:: [MetadataType(typeof(CarMetadata))] public partial class Car { } [DataContract("Automobile")] public partial class CarMetadata { [DataMember] public string Model {get; set;} } When I run the application the

Compare attribute in nested class

馋奶兔 提交于 2019-12-11 06:16:09
问题 I want to reuse my User class in my UserViewModel because it is too big, with too many properties. public class User { [DataType(DataType.EmailAddress)] public string Email { get; set; } [DataType(DataType.Password)] public string Password { get; set; } /* ... many other properties */ } My UserViewModel have the property User and the ConfirmEmail and ConfirmPassword properties. public class UserViewModel { public User User; [DataType(DataType.EmailAddress)] [Compare("User.Email")] public

How to get SelectList to honor DisplayName annotation with enums?

百般思念 提交于 2019-12-11 05:50:07
问题 I am using enums in my ViewModels to populate a DropDownListFor like this: @Html.DropDownListFor(model => model.SelectedDropDownValue, new SelectList(Enum.GetValues(typeof(SearchOptions)), SearchOptions.SSN)) This works well. However, I would like to display the DisplayName property of the DisplayNameAttribute that is associated with each value within the enum. I have search Stackoverflow and seen a number of ways to do this with helpers, extensions, etc. Is it possible to, within a single

Keeping IClientValidatable outside the model layer

安稳与你 提交于 2019-12-11 05:41:14
问题 I'm using Data Annotations to validate my model classes. I wrote a couple of custom attributes as well. Ultimately, the model is pushed to a web interface built in ASP.NET MVC, but I want to keep a clean separation of concerns, so the model classes has its own assembly (which will also be used by console apps). Having to use the IClientValidatable interface (which is a web concern) in the model layer breaks the loose coupling I'm aiming for. Any ideas on how to fix this? Thanks. 回答1: You can

ASP.NET Core: ShortName in the Display attribute (DataAnnotations)

有些话、适合烂在心里 提交于 2019-12-11 05:29:00
问题 In an ASP .NET Core 1.1 project (VS 2017) I try to use the ShortName attrubute of the Display property in order to use the DisplayFor HTML Helper: [Display(Name="Project Name", ShortName="Name", Description="The name of the project")] public string Name { get; set; } I read the following answer that does the trick for the Description . Unfortunately for a reason I don't understand, this doesn't work for the ShortName . There is the code I tried, the first method seems OK, but the second does