Why is CompareAttribute in the MVC namespace and not the DataAnnotations namspace? - asp.net-mvc-3

MVC 3 includes a new validation attribute called CompareAttribute.
But why is this validation attribute in the mvc namespace and not with all of the other validation attributes in the DataAnnotations namespace?
Are the other validation attributes spread throughout other namespaces?

It's just the way things worked out. Derived types do not have to be in the same namespace as the base type. DataAnnotations is part of the .NET framework which releases a lot less frequently than a standalone project like ASP.NET MVC.
It's possible that there are types derived from ValidationAttribute in other namespaces, but most are part of the core DataAnnotations.

Related

What is the IShouldNormalize's alternative in Abp.io?

I have a project, that created in Abp.io framework. I before used ASP.NET Boilerplate framework and ASP.NET Boilerplate has IShouldNormalize interface for normalize request's input. But I don't find IShouldNormalize's alternative in Abp.io
What is the IShouldNormalize's alternative in Abp.io ?
No alternative. You can use IValidatableObject for validation and
simple normalization. You can add your own method and call in the
application service.
But we suggest you to make it in your application service method
explicitly
https://github.com/abpframework/abp/issues/1908
You can use the IValidatableObject for validation and normalization for your needs.
IValidatableObject has just a method named Validate you can define your normalization rules in here, that you normally do in the Normalize method of theIShouldNormalize interface.

How to implement custom validation in Blazor

How I can implement validation to my model, but not use attributes in this class, because, this class has some behavior of dependency on how to fill properties.
For example, if property IsDropDown is true, I must validate only DropDownValue else I must validate other properties.
Use FluentValidation
Read Steve Sanderson's Blog : Integrating FluentValidation with Blazor
And Chris Sainty's article : Using FluentValidation for Forms Validation in Blazor

Does ASP.NET web API support IValidatableObject?

I have a view model that implements IValidatableObject and also has several validation attributes. When I attempt to call an action on my ApiController, only the attribute validation is performed. Does ASP.NET Web API not support IValidatableObject? What's the alternative for complex validation that cannot be represented by a single attribute?
Edit: Somewhere along the line, I must have fudged something up. The validation mysteriously started working as expected. Looks like IValidatableObject is definitely supported by default.
With Web API 2.1 (Microsoft.AspNet.WebApi nuget 5.1.x), I experienced IValidatableObject's Validate method not being called if any of the validation attributes are invalid. Hence, all validation attributes that have been applied to your class's properties must first pass as valid before that class's Validate method will be called.
If, for example, you have a property with the RequiredAttribute and you do not put a value in that field, your implementation of IValidatableObject's Validate method will not be called. Although not technically a bug, I expected the Validate method to be called every time I validate.
Not yet tried IValidatableObject on webapi, but it should be supported according to the documentation the Validation provider for DataAnnotations (DataAnnotationsModelValidatorProvider) provide also IValidatableObject validation. See here: http://msdn.microsoft.com/en-us/library/system.web.http.validation.providers(v=vs.108)
Anyway, you can use also Object level ValidationAttribute that you can use to decorated a class...It is not so easy as IValidatableObject, but should work.
As of now, IValidatableObject is supported.

In ASP.NET MVC 3, what class does Views and Partial Views inherit from?

In ASP.NET MVC 3, what class doe Views and Partial Views inherit from and can the class (presumably it's a class somewhere) be inherited from to extend functionality.
For example, the default LogOnPartial control has the following (using Razor syntax)
Welcome <b>#Context.User.Identity.Name</b>!
Where is the Context object exposed. What class makes that available to the partial view?
They inherit from System.Web.Mvc.WebViewPage. http://msdn.microsoft.com/en-us/library/system.web.mvc.webviewpage%28VS.98%29.aspx In older versions of razor it was required to add the #inherits but newer versions don't.

Contextual/RunWhen validation in ASP.NET MVC 2?

Does the latest ASP.NET MVC 2 validation allow contextual validation? I'm looking for something similar to Castle Validator's "RunWhen" property. It allows you to declare that a validator should only be executed in a particular context.
The most obvious use is for Identity fields. The following would specify that the int ID field is required, but only for updates (maybe for deletes too). It should never be required on an insert, however.
[Required(RunWhen=RunWhen.Update)]
public int ID {...}
This seems like a very common scenario. How can this be handled with the out-of-the-box asp.net mvc 2 validation?
No out of the box, but you can plug Yourself in.
Read this one.

Resources