|Published (Last):||19 September 2013|
|PDF File Size:||19.18 Mb|
|ePub File Size:||4.72 Mb|
|Price:||Free* [*Free Regsitration Required]|
Read more at Introducing ASP. I have solved this by handling the "ModelState" dictionary, which is contained by the controller. The ModelState dictionary includes all the members that have to be validated. If you need to implement a conditional validation based on some field e. With this we achieve conditional validation, while leaving everything else the same. This is my final implementation: I have used an interface on the model and the action attribute that validates the model which implements the said interface.
I did not want to complicate this answer, so I did not mention the final implementation details which, at the end, matter in production code. I had the same problem yesterday but I did it in a very clean way which works for both client side and server side validation.
Condition: Based on the value of other property in the model, you want to make another property required. Here is the code. Here PropertyName is the property on which you want to make your condition DesiredValue is the particular value of the PropertyName property for which your other property has to be validated for required. At last but not the least , register adapter for your attribute so that it can do client side validation I put it in global.
I've been using this amazing nuget that does dynamic annotations ExpressiveAnnotations. You put that directly on the property you want to validate, so you get the desired behavior of the validation error being associated to the "Senior" property. You need to validate at Person level, not on Senior level, or Senior must have a reference to its parent Person. It seems to me that you need a self validation mechanism that defines the validation on the Person and not on one of its properties.
I'm not sure, but I don't think DataAnnotations supports this out of the box. What you can do create your own Attribute that derives from ValidationAttribute that can be decorated on class level and next create a custom validator that also allows those class-level validators to run. I know Validation Application Block supports self-validation out-of the box, but VAB has a pretty steep learning curve.
Nevertheless, here's an example using VAB:. I had the same problem, needed a modification of [Required] attribute - make field required in dependence of http request.
The solution was similar to Dan Hunex answer, but his solution didn't work correctly see comments. I don't use unobtrusive validation, just MicrosoftMvcValidation. Here it is. Implement your custom attribute:. The main advantage for me is that I don't have to code custom client validator as in case of unobtrusive validation. In your example, keep everything as is and add the logic suggested to your Controller's Action. I'm assuming your ViewModel passed to the controller action has the Person and Senior Person objects with data populated in them from the UI.
Then you just need to check the validation on your post action. Learn more. Asked 10 years, 2 months ago. Active 4 months ago. Viewed k times. How to use data annotations to do a conditional validation on model? Andrew Arnold 4, 11 11 gold badges 36 36 silver badges 53 53 bronze badges.
Peter Stegnar Peter Stegnar I've recently asked similar question: stackoverflow. I'm confused. A Senior object is always a senior, so why can IsSenior be false in that case. Don't you just need the 'Person. Senior' property to be null when Person. IsSenior is false. Steven: "IsSenior" translates to the checkbox field in the view. When user checks the "IsSenior" checkBox then the "Senior.
Description" Field become mandatory. Darin Dimitrov: Well sort of, but not quite. You see, how would you achieve that the the error mesage is appent to the specific field?
If you validate at object level, you get an error at object level. I need error on property level. Active Oldest Votes. IsNullOrEmpty Senior. Description yield return new ValidationResult "Description must be supplied. James Skemp 6, 9 9 gold badges 52 52 silver badges 86 86 bronze badges. Unfortunately, Microsoft put this in the wrong layer - validation is business logic and this interface is in the System.
Web DLL. In order to use this, you have to give your business layer a dependency on a presentation technology. RayLoveless you should be calling ModelState. Here is the solution: If you need to implement a conditional validation based on some field e.
In some class In some controller action UPDATE: This is my final implementation: I have used an interface on the model and the action attribute that validates the model which implements the said interface. Jakov 8 8 silver badges 21 21 bronze badges. The downside is that one of part your validation logic is located in the model and the other part in the controller s. Well of course this is not necessary. I just show the most basic example.
I have implemented this with interface on model and with action attribute that validates model which implements the mentioned interface.
Are you able to expand on this answer? This makes some sense, but I want to make sure I'm crystal on it. I'm faced with this exact situation, and I want to get it resolved. GetProperty PropertyName. GetValue instance, null ; if proprtyvalue. Ciaran Bruen 4, 13 13 gold badges 51 51 silver badges 65 65 bronze badges. Dan Hunex Dan Hunex 4, 1 1 gold badge 20 20 silver badges 30 30 bronze badges.
This is was the original starting point miroprocessordev. Is there any equvalent solution in asp. ValidationResult, ValidationContext classes are not available in asp. I managed to get this working in the client side with MVC5, but in client it fires up the validation no matter what the DesiredValue is.
Any help pls? Korayem Korayem 9, 5 5 gold badges 60 60 silver badges 51 51 bronze badges. The ExpressiveAnnotation library is the most flexible and generic solution of all answers here.
Thanks for sharing! I've been banging my head trying to find a solution for a solid day. ExpressiveAnnotations looks to be the fix for me! ExpressiveAnnotation library is awesome!
It has client side support too! No support for. NET Core though, and doesn't look like it'll happen. Clear ;.
A TÁMOP-412/08/1/A-2009-0046 támogatásával
Solar irrigation measuring range. Frequency of. Parameters and Characteristics. The most common example in usage being the SL transistor.
Rendszerindítás vállalati adatokkal – GYIK