data-annotations

Different model requirements for POST and PUT

假装没事ソ 提交于 2019-11-30 21:19:01
Say I have a controller CatController with actions for GET, POST and PUT. They all use the same Cat resource which could look like this: public class CatDto { public int Id { get; set; } [Required] public string Name { get; set; } [Required] public bool IsFriendly {get; set; } } However, the Name and IsFriendly properties should only be required when creating a new cat (POST), but optional when updating it (PUT) to allow updating only a single property. The way I've handled this until now is simply having two classes, a CreateCat and UpdateCat which have the same properties but different data

How to call ValidationAttributes manually? (DataAnnotations and ModelState)

半腔热情 提交于 2019-11-30 20:32:12
We have a need within some of our logic to iterate through the properties of a model to auto-bind properties and want to extend the functionality to include the new dataannotations in C# 4.0. At the moment, I basically iterate over each property loading in all ValidationAttribute instances and attempting to validate using the Validate/IsValid function, but this doesn't seem to be working for me. As an example I have a model such as: public class HobbyModel { [Required(AllowEmptyStrings = false, ErrorMessage = "Do not allow empty strings")] [DisplayName("Hobby")] [DataType(DataType.Text)]

Data validation with custom attributes (AttributeTargets.Class) on EF buddy classes

天大地大妈咪最大 提交于 2019-11-30 20:00:02
问题 I have an Entity Framework generated class with the following properties: public DateTime LaunchDate; public DateTime ExpirationDate; I need to enforce that ExpirationDate > LaunchDate. I am using a buddy class as described in various posts. I am applying custom validation attributes (on properties) to other properties in the same buddy class and these are working. Since I need to compare two properties I am using an attribute directly on the class (AttributeTargets.Class) Here is my custom

FileExtensions attribute of DataAnnotations not working in MVC

对着背影说爱祢 提交于 2019-11-30 19:18:05
I am trying to upload a file using HTML FileUpload control in MVC. I want to validate the file to accept only specific extensions. I have tried using FileExtensions attribute of DataAnnotations namespace, but its not working. See code below - public class FileUploadModel { [Required, FileExtensions(Extensions = (".xlsx,.xls"), ErrorMessage = "Please select an Excel file.")] public HttpPostedFileBase File { get; set; } } In the controller, I am writing the code as below - [HttpPost] public ActionResult Index(FileUploadModel fileUploadModel) { if (ModelState.IsValid) fileUploadModel.File.SaveAs

How can I ignore case in a RegularExpression?

我的梦境 提交于 2019-11-30 18:42:51
I have an asp.net MVC application. There is an entity called File that it has a property called Name. using System.ComponentModel.DataAnnotations; public class File { ... [RegularExpression(@"([^.]+[.](jpg|jpeg|gif|png|wpf|doc|docx|xls|xlsx ..., ErrorMessage = "Invali File Name"] public string Name{ get; set; } ... } There is a RegularExpressionValidator that checks file extensions. Is there a quick way I can tell it to ignore the case of the extension without having to explicitly add the upper case variants to my validation expression? I need this RegularExpressionValidator for both Server

ASP.NET MVC displaying date without time

爱⌒轻易说出口 提交于 2019-11-30 18:18:56
I have my model field decorated in the following way: [DataType(DataType.Date)] [Display(Name = "Date of birth")] public string DateOfBirth { get; set; } When I want to display the value in the view using the following code: <%: Html.DisplayFor(m => m.DateOfBirth) %> The problem is that the date is displayed together with its time value. I wonder why it does not take DateType attribute into consideration and displays only the date value without time. I know that I may create a display template for DateTime but in other cases than date of birth I want to show time together with date. How to

DataAnnotations on public fields vs properties in MVC

南笙酒味 提交于 2019-11-30 18:04:19
问题 Why don't DataAnnotations work on public fields? Example: namespace Models { public class Product { [Display(Name = "Name")] public string Title; // { get; set; } } } public ActionResult Test() { return View(new Models.Product() { Title = "why no love?" }); } @Html.LabelFor(m => m.Title) // will return 'Title' if field, or 'Name' if property @Html.DisplayFor(m => m.Title) If Title is a field, then the Display attribute seems to have no effect. If Title is changed to a property, it works as

Entity Framework Data Annotations equivalent of .WillCascadeOnDelete(false);

 ̄綄美尐妖づ 提交于 2019-11-30 17:40:26
I'm currently using EF Code First 4.3 with migrations enabled, but automatic migrations disabled. My question is simple, is there a data annotations equivalent of the model configuration .WillCascadeOnDelete(false) I would like to decorate my class so that the foreign key relationships do NOT trigger a cascading delete. Code sample: public class Container { public int ContainerID { get; set; } public string Name { get; set; } public virtual ICollection<Output> Outputs { get; set; } } public class Output { public int ContainerID { get; set; } public virtual Container Container { get; set; }

Validation of Guid

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 17:38:17
I have a strongly-typed view which has a DropDownListFor attribute on it. Each item in the dropdown list is represented by a GUID. What I'm after is a way to validate if a user selects an item from the dropdown list. At present i don't see anyway of doing this using Data Annotations. Is there anyway of achieving this using Data Annotations so client and server side validation would work. I'm guessing i need to make a custom method to do this but was wondering if anything already existed. Actually, you can't use Required attribute with GUIDs (without the method I mention below) because they

Server side validation with custom DataAnnotationsModelValidatorProvider

烂漫一生 提交于 2019-11-30 15:28:34
I have setup a custom provider to allow setting validation attributes from a data store instead of in static code. Works great with the client side validation in my .NET MVC 4 project, but I am unable to get the server side validation to work. CustomModelValidatorProvider .cs: public class CustomModelValidatorProvider : DataAnnotationsModelValidatorProvider { protected override IEnumerable GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable attributes) { // set attributes from datastore here return base.GetValidators(metadata, context, attributes); } } In my Global