model-validation

Validation Attribute get triggered two times

我是研究僧i 提交于 2019-12-11 03:45:36
问题 In my MVC3 application I have the model ( not important properties deleted ): public class AccountViewModel { [StringLength(65)] public string Property1 { get; set; } [StringLength(65)] public string Property2 { get; set; } } The problem is when an action is submited validation attribute called twice, and I can get 4 errors in summary, instead of 2: 'Property1' length must be less than 65 characters 'Property1' length must be less than 65 characters 'Property2' length must be less than 65

How to get model validation to pickup attributes set on objects in a List in MVC3?

陌路散爱 提交于 2019-12-10 19:24:42
问题 I have a set of models that looks similar to this public class OtherModel { [Required] string name { get; set; } } public class OthersEditModel { List<OtherModel> others { get; set; } } I then have a controller method that looks like this [HttpPost] public ActionResult EditOthers(OthersEditModel others) { if(ModelState.IsValid) { // Save } } My problem is that the ModelState.IsValid isn't triggering the validation of the objects in the list. How do I accomplish this, or is it even possible?

Create a complex type model validation attribute with server and client validation

前提是你 提交于 2019-12-10 10:59:05
问题 I'm trying to create an attribute that can validate a complex type both on the server and client side. This attribute will be used for required and non required complex types such as the following Address Class public partial class AddressViewModel { [DisplayName("Address 1")] [MaxLength(100)] public virtual string Address1 { get; set; } [DisplayName("Address 2")] [MaxLength(100)] public virtual string Address2 { get; set; } [MaxLength(100)] public virtual string City { get; set; } [MaxLength

Data Member Validation - ISO 4217 (currency) and 639-1 (language)

做~自己de王妃 提交于 2019-12-08 06:47:56
问题 So, I am looking to do some custom Data Field Validation on some models in the .NET project I am currently working on. These involve a Default Language and a Default Currency. In order to meet the standards of this application and the various other applications and services involved, the currency needs to be a ISO 4217 (3 character currency) standard and the language needs to be a ISO 639-1 (2 character language). Seeing as no model validation is being done for this anywhere yet, I have the

Auto generating metadata classes for Entity Framework

别说谁变了你拦得住时间么 提交于 2019-12-07 11:00:32
问题 I am considering using xVal for validation of Entity Framework classes in a MVC application. This involves writing metadata classes as explained in details by Graham O'Neale (http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework). I am wondering if there's a way to auto generate such metadata classes using the metadata from the SQL database (for example: not null fields will have [Required] class attribute. 回答1: You could use Code Smith tool www

Create a complex type model validation attribute with server and client validation

人盡茶涼 提交于 2019-12-06 14:32:01
I'm trying to create an attribute that can validate a complex type both on the server and client side. This attribute will be used for required and non required complex types such as the following Address Class public partial class AddressViewModel { [DisplayName("Address 1")] [MaxLength(100)] public virtual string Address1 { get; set; } [DisplayName("Address 2")] [MaxLength(100)] public virtual string Address2 { get; set; } [MaxLength(100)] public virtual string City { get; set; } [MaxLength(50)] public virtual string State { get; set; } [MaxLength(10)] [DisplayName("Postal Code")] public

Auto generating metadata classes for Entity Framework

假装没事ソ 提交于 2019-12-05 13:40:23
I am considering using xVal for validation of Entity Framework classes in a MVC application. This involves writing metadata classes as explained in details by Graham O'Neale ( http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework ). I am wondering if there's a way to auto generate such metadata classes using the metadata from the SQL database (for example: not null fields will have [Required] class attribute. You could use Code Smith tool www.codesmithtools.com (there is a free version if I remember correctly) I wrote an application that will read

How to perform async ModelState validation with FluentValidation in Web API?

情到浓时终转凉″ 提交于 2019-12-04 09:07:47
I setup a web api project to use FluentValidation using the webapi integration package for FluentValidation. Then I created a validator that uses CustomAsync(...) to run queries against the database. The issue is that the validation seems to deadlock when awaiting for the database task. I did some investigation, it seems that the MVC ModelState API is synchronous, and it calls a synchronous Validate(...) method that makes FluentValidation to call task.Result , causing the deadlock. Is it correct to assume that async calls won't work well with webapi integrated validation? And if that is the

Combine two regular expression into one while validating Attribute

一曲冷凌霜 提交于 2019-12-02 07:31:52
问题 I have two regular expression. [RegularExpression(@".*[^ ].*", ErrorMessage ="Something")] validate string that only contains spaces(Not any other characters Ex: " ".length = 7 ). [RegularExpression(@"^[^~!@#$%&*]+$", ErrorMessage = "something")] validate string that contains ~!@#$%&* special characters. How can I combine both regex into one, because Duplicate Regular expression annotation is not allowed in asp.net mvc. 回答1: You may use ^[^~!@#$%&*]*[^~!@#$%&*\s][^~!@#$%&*]*$ See the regex

Combine two regular expression into one while validating Attribute

假如想象 提交于 2019-12-02 07:05:09
I have two regular expression. [RegularExpression(@".*[^ ].*", ErrorMessage ="Something")] validate string that only contains spaces(Not any other characters Ex: " ".length = 7 ). [RegularExpression(@"^[^~!@#$%&*]+$", ErrorMessage = "something")] validate string that contains ~!@#$%&* special characters. How can I combine both regex into one, because Duplicate Regular expression annotation is not allowed in asp.net mvc. You may use ^[^~!@#$%&*]*[^~!@#$%&*\s][^~!@#$%&*]*$ See the regex demo Details ^ - start of string [^~!@#$%&*]* - 0+ chars other than a char in the ~!@#$%&* list [^~!@#$%&*\s]