data-annotations

ASP.NET Core is using non-default constructor if no public default constructor found

偶尔善良 提交于 2019-12-08 05:11:45
问题 I am writing ASP.NET Core 2.2.0 application hosted on Service Fabric. I have a class which represents a request and I have declared two constructors: public for my own usage and private for serializers: public class MyClass { private MyClass() // for serializer { } public MyClass(string myProperty) // for myself { MyProperty = myProperty ?? throw new ArgumentNullException(nameof(myProperty)); } [Required] public string MyProperty { get; private set; } } Then, I created an API controller:

Accessing Custom Attributes in a T4 Template (VS2012 MVC4)

走远了吗. 提交于 2019-12-08 02:41:21
问题 I'm trying to access my custom attributes like SortableAttribute attached to my model properties in a T4 Controller template I'm writing. I've copied the class block, assemblies and imports from List.tt as boilerplate. First I tried to cast the attributes as follows: <#+ string SortableBy(PropertyInfo property) { foreach (object attribute in property.GetCustomAttributes(true)) { var sortable = attribute as SortableAttribute; if (sortable != null) return sortable.By == "" ? property.Name :

how to validate dropdownlist using the DataAnnotation?

巧了我就是萌 提交于 2019-12-08 00:19:50
问题 I need your help, I have a problem with the validation using AdataAnnotation I am trying to validate a dropdown list using it but there is some problem with it this is my code View Side @using (Html.BeginForm("addNewProject", "Activities", FormMethod.Post)) { @Html.AntiForgeryToken() @Html.ValidationMessage("ProjectName") <h3>Project Name: </h3> @Html.TextBox("ProjectName", null, new { @class = "text_field"} ) @Html.ValidationMessage("ProjectOwner") <h3>Project Owner: </h3> @Html.DropDownList

ASP.NET MVC Web API data annotation regular expression for email

不想你离开。 提交于 2019-12-08 00:08:59
问题 Using [RegularExpression(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$")] attribute never validates an email property of my model. I'm new to regular expressions so I can't figure out what is the problem. Little help? 回答1: Look at this article: How To Validate Email Address With Regular Expression. The guy not only providing you with the regular expression, but also explaining step by step what it means. Not only its useful as an expression, but you can also learn a thing or two from

Validator.TryValidateObject does not call Remote validation

試著忘記壹切 提交于 2019-12-07 18:28:17
问题 I have a class with a remote validation data annotation as follows: public partial class LuInspectionWindow { [Required] public int InspectionWindowId { get; set; } [Required] public string Description { get; set; } [Required] [DataType(DataType.Date)] public DateTime StartDate { get; set; } [Required] [Remote("ValidateWindowEndDate", "InspectionWindow", AdditionalFields = "StartDate")] public DateTime EndDate { get; set; } } That calls this annotation: [AcceptVerbs("Get", "Post")] public

Is it possible to “hack into” the unobtrusive validaton supported in ASP.NET MVC 3?

纵饮孤独 提交于 2019-12-07 15:20:08
问题 I want to be able to use the built-in, data-annotation based, client-side unobtrusive validation, but do my own ajax form submission after I know it passes. Something like this jQuery bit: $('#form').submit(function(e) { e.preventDefault(); if (PassesUnobtrusiveValidation()) { // ajax submit form } }); Is there a PassedValidation event, or something like it, (built into the default, asp.net mvc 3 data-annotation based client side validation) I can hook into (like the psuedocode in the example

How to specify order of Data Annotation errors in Html.ValidationSummary

牧云@^-^@ 提交于 2019-12-07 13:45:07
问题 I'm displaying errors on my form with the use of <%= Html.ValidationSummary("Please review the errors below") %> My domain object inherits from a base class and I am finding that the base class data annotation properties are being displayed at the bottom of the list. This goes against the order in which they appear in my form. Is there any way of specifying what order the errors should be displayed? Example: public class ClassA { [Required]public string AProperty; } public class ClassB :

Custom ValidationAttribute doesn't work. Always returns true

六月ゝ 毕业季﹏ 提交于 2019-12-07 13:42:09
问题 I've created a custom ValidationAttribute class to check the age of a person in my application: public class MinimumAgeAttribute : ValidationAttribute { public int MinAge { get; set; } public override bool IsValid(object value) { return CalculateAge((DateTime) value) >= MinAge; } private int CalculateAge(DateTime dateofBirth) { DateTime today = DateTime.Now; int age = today.Year - dateofBirth.Year; if (dateofBirth > today.AddYears(-age)) age--; return age; } } The data annotation is set on

C# adding DataAnnotations to entities from the EntityFramework

孤街醉人 提交于 2019-12-07 13:33:38
问题 I'm using the ADO.Net Entity Framework. To handle input validation I'm trying to use DataAnnotations , I looked around on StavkOverflow and Google, and everywhere I found almost the same example of using MetadataType . However, I've been trying for hours and I cannot get it to work.. For some reason, the CustomAttributes from the EmployeeMetaData class are not being applied to the respective field/properties on the Employee class. Does anyone have any idea why this might be happening? And yes

Entity Framework Core, DELETE CASCADE, and [Required]

这一生的挚爱 提交于 2019-12-07 11:58:24
问题 I am running into an issue DELETE CASCADE in Entity Framework Core that I can't seem to find a good solution to. Here's a super simplified version of my model: User {UserID, Name} Recipe {RecipeID, UserID} Ingredient {IngredientID, UserID} RecipeIngredient {RecipeID, IngredientID} *RecipeIngredient is the many-to-many table. Recipe and Ingredient both have UserID marked as [Required], and RecipeIngredient has RecipeID and IngredientID marked as [Required]. The issue is that SQL will not