data-annotations

Validator.TryValidateObject does not validate attribute

时光怂恿深爱的人放手 提交于 2019-12-24 05:38:27
问题 Using WebApi v2 I've built something that takes an object as argument. I'm using both IValidateableObject and dataannotations for model validations that I trigger using an WebApi filter. However, one object contains an array of items that all need to be validated. I've created a custom attribute in order to do this using something like Asp.net Web Api nested model validation, but I can't get the validation going. Also - I've use the ValidateAllProperties flag already. So I built a console app

Validator.TryValidateObject does not validate attribute

坚强是说给别人听的谎言 提交于 2019-12-24 05:38:07
问题 Using WebApi v2 I've built something that takes an object as argument. I'm using both IValidateableObject and dataannotations for model validations that I trigger using an WebApi filter. However, one object contains an array of items that all need to be validated. I've created a custom attribute in order to do this using something like Asp.net Web Api nested model validation, but I can't get the validation going. Also - I've use the ValidateAllProperties flag already. So I built a console app

When Adding Data Annotation Attribute it's throwing Exception

╄→尐↘猪︶ㄣ 提交于 2019-12-24 05:24:10
问题 public class Employee { [Key] public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public decimal Salary { get; set; } public string Email { get; set; } } public class EmployeeContext : DbContext { public DbSet<Employee> Employees { get; set; } } When I'm adding Data Annotation [Required(ErrorMessage = "Employee Name is required")] to the Name property it's throwing an InvalidOperationException . As I was trying to fix the bug I'm getting these

Override dataType mismatch message of property in asp.net mvc(and display message through resource file)

拈花ヽ惹草 提交于 2019-12-24 03:43:11
问题 want to override datetime "wrong format message". i.e by default DateTime property display message:: The value '12--2002' is not valid for * dateTime PropertyName: (DD/MM/YYYY). But i want this message to display through some Resource file. public class candidateAddressViewModel { [Required(ErrorMessageResourceType = typeof(myApp.Resources.res.Resource), ErrorMessageResourceName = "error_format")] [Display(Name = "lived_from", ResourceType = typeof(myApp.Resources.res.Resource)),

Override dataType mismatch message of property in asp.net mvc(and display message through resource file)

我的梦境 提交于 2019-12-24 03:43:05
问题 want to override datetime "wrong format message". i.e by default DateTime property display message:: The value '12--2002' is not valid for * dateTime PropertyName: (DD/MM/YYYY). But i want this message to display through some Resource file. public class candidateAddressViewModel { [Required(ErrorMessageResourceType = typeof(myApp.Resources.res.Resource), ErrorMessageResourceName = "error_format")] [Display(Name = "lived_from", ResourceType = typeof(myApp.Resources.res.Resource)),

ASP.NET MVC: DataAnnotations - Show an error message indicating that a field must be numeric

匆匆过客 提交于 2019-12-24 00:53:05
问题 There appears to be something of a hole in the way DataAnnotations works in that a user entering in some text into a field that will go into an int will never reach the DataAnnotations code. It kicks off a model binding error and displays the error to the user "The value 'a' is not valid for the XXXX field." Anyway, it's all very nice that it automatically handles this situation, but I actually want to display an error message indicating the problem eg. "The value 'a' is not numeric. Please

Pass variable data to ValidationAttribute

对着背影说爱祢 提交于 2019-12-24 00:36:23
问题 I have a requirement to apply different min length values in a change password function to a new password being created based on a user's roles. If a user has no administrative roles min length is 12, if they have admin roles min length is 16. The current code has no such variable requirement logic. The implementation for the new password property was like so in model class called ChangePasswordData: ///summary> /// Gets and sets the new Password. /// </summary> [Display(Order = 3, Name =

Custom DataAnnotation for Multiple Checkboxes

£可爱£侵袭症+ 提交于 2019-12-24 00:19:00
问题 Alright, so I have these checkboxes of Products and I want to make sure at least one Product is selected. To do this, my ViewModel contains: [DisplayName(@"Product Line")] [MinChecked(1)] public List<CheckboxInfo> ActiveProducts { get; set; } The View simply contains: @Html.EditorFor(x => x.ActiveProducts) That EditorTemplate contains: @model Rad.Models.CheckboxInfo @Html.HiddenFor(x => x.Value) @Html.HiddenFor(x => x.Name) @Html.CheckBoxFor(x => x.Selected) @Html.LabelFor(x => x.Selected,

Set Initial Value for Required attribute

风格不统一 提交于 2019-12-23 23:46:06
问题 Using Data Annotations to specify validation rules for a model, I want to set the initial value for the Required attribute so that it fails if it's equal to that value. This was an option in webforms using the validation controls, but I don't see similar using Data Annotations. If there's no built in way to do it, I'll probably just build a new Required Attribute that inherits from it. UPDATE : Based on comments/answers, I definitely know this is not an ideal situation and I should be using

Change how [DataType.Currency] renders html

谁说我不能喝 提交于 2019-12-23 18:29:57
问题 Currently, when applying the DataAnnotation Currency to a property, it then using DisplayFor renders this as the html : cshtml: <div>@Html.DisplayFor(m=>m.Price)</div> html: <div>$U 4.193,99</div> How can I change it so the currency symbol is not white-spaced ? I know I can apply a css to the div for white-space:nowrap; but it would be better if I can just change the currency string format to get : <div>$U 4.139,99</div> 回答1: You can use a custom display format (instead of the "Currency"