data-annotations

Custom ValidationAttribute test against whole model

ぃ、小莉子 提交于 2019-11-29 18:07:10
I know this is probably not possible but let's say I have a model with two properties. I write a ValidationAttribute for one of the properties. Can that VA look at the other property and make a decision? So; public class QuickQuote { public String state { get; set; } [MyRequiredValidator(ErrorMessage = "Error msg")] public String familyType { get; set; } So in the above example, can the validator test to see what's in the "state" property and take that into consideration when validating "familyType"? I know I can probably save the object to the session but would like to avoid any saving of

MVC data annotations range validation not working properly

♀尐吖头ヾ 提交于 2019-11-29 16:37:37
问题 I have a RangeValidator on a property in my model to only allow Integers that are between 0 and 100. I have a partial view that displays a form to update the property via a jQuery UI dialog. I have looked at the source and can confirm that the data annotation attributes are being generated properly. However, the validatation isn't working properly. It does perform some kind of validation, but it isn't using the range I'm setting. The values 1, 10, and 100 do not produce the error. Any other

How to use ASP.Net core ModelMetadata attribute

喜欢而已 提交于 2019-11-29 16:10:29
[Table("LegalEntity")] [ModelMetadataType(typeof(LegalEntityMeta))] public class LegalEntity : Entity<long> { } public class LegalEntityMeta { [JsonProperty(PropertyName = "LegalEntityId")] public long Id { get; set; } [JsonProperty(PropertyName = "LegalEntityName")] public string Name { get; set; } } In the Startup.cs .... services .AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }) .AddAutoMapper(typeof(Startup)) .AddMvcCore() .AddJsonFormatters() .AddApiExplorer(); My expectation is to see

Integer validation against non-required attributes in MVC

狂风中的少年 提交于 2019-11-29 14:49:34
I've trying to validate a property on a model I have. This property is NOT required, and so if its invalid MVC seems to be ignoring it. I've even created a custom ValidationAttribute, but nothing works. public class NumberWang : ValidationAttribute { public override bool IsValid(object value) { if (value == null) return true; int g; if (int.TryParse(value.ToString(), out g)) { if (g >= 0) return true; } return false; } } public class MyModel { [Range(0, 999999, ErrorMessage = "category_id must be a valid number")] [NumberWang(ErrorMessage = "That's NumberWang!")] public int? category_id { get;

Can't get MVC 4 FileExtensions Attribute to work on ViewModel property

左心房为你撑大大i 提交于 2019-11-29 13:57:21
I need to upload a csv file and I want to restrict it's extension to .csv So I added the follow property to my ViewModel: [FileExtensions(ErrorMessage = "Must choose .csv file.",Extensions = "csv,txt")] public HttpPostedFileBase File { get; set; } In my view I have the following: @Html.TextBoxFor(m => m.File, new { type = "file"}) @Html.ValidationMessageFor(m => m.File) However as soon as it hits my "ModelState.IsValid" check it returns invalid with my error message of "Must choose .csv file." I assume I'm just missing a parameter, but I haven't found a sample of this in use any where yet.

StringLengthAttribute doesn't seem to be working

安稳与你 提交于 2019-11-29 13:33:15
问题 Here is my Test class with data annotations: class Test { [Required, StringLength(10)] public string MyProperty { get; set; } } Here is my console test program: class Program { static void Main(string[] args) { var test = new Test { MyProperty = "this is way more than 10 characters and should fail." }; var context = new ValidationContext(test, null, null); // No exception here! (why not?) Validator.ValidateObject(test, context); test.MyProperty = null; // Exception here, as expected Validator

ASP.NET MVC ModelMetaData: Is there a way to set IsRequired based on the RequiredAttribute?

偶尔善良 提交于 2019-11-29 12:06:00
问题 Brad Wilson posted a great blog series on ASP.NET MVC's new ModelMetaData: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html In it, he describes how the ModelMetaData class is now exposed in the Views and templated helpers. What I'd like to do is display an asterisk beside a form field label if the field is required, so I thought about using the IsRequired property of ModelMetaData. However, IsRequired by default is true for all non-nullable

dataannotations set identity seed value on Primary Key with code first

落花浮王杯 提交于 2019-11-29 11:33:10
ASP.NET MVC3 code first project. In my class definition how do I set the Identity seed value. public class Account { [Key] public int Id { get; set; } What would be the syntax to set the Identity seed to 1000000? Thank you Joe Thanks Craig, after looking at https://stackoverflow.com/a/5974656/968301 it was pretty simple. Create an intializer public class MyInitializer : DropCreateDatabaseIfModelChanges<MyContext> { protected override void Seed(MyContext context) { context.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Account', RESEED, 1000000)"); } } Then call from the Application_Start

How to bind view model property with different name

*爱你&永不变心* 提交于 2019-11-29 11:31:46
问题 Is there a way to make a reflection for a view model property as an element with different name and id values on the html side. That is the main question of what I want to achieve. So the basic introduction for the question is like: 1- I have a view model (as an example) which created for a filter operation in view side. public class FilterViewModel { public string FilterParameter { get; set; } } 2- I have a controller action which is created for GETting form values(here it is filter) public

How would you unit test data annotations?

一世执手 提交于 2019-11-29 10:58:55
Two of the class properties have the following annotations: [Key] [Column] [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [MaxLength(25)] public string Name { get; set; } I understand that testing Key, Column and Required attributes is no longer a unit test, it's an integration test as it would depend on the underlying database, but how do you go about testing MaxLength(25) attribute? One of the alternatives that I can think of, is to add a code contract into the property. Update As suggested, I wrote the following helper: public class