IModelBinder with DataAnnotation Support - asp.net-mvc-3

We have to use a custom ModelBinder that implements IModelBinder. We cannot directly change this code at all. It currently doesn't have support for updating ModelState from the DataAnnotations of the model passed in. We want to subclass the ModelBinder and add that support. What do we need to do to add that support so it works like DefaultModelBinder works with 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 temporarily disable [JsonIgnore] attribute?

I am working on a ASP.NET WebApi project. I have a model class that has a property that I don't want to serialize in public calls. For this purpose I am usin [JsonIgnore] for that property and it is excluded from jsons as expected. Now in a [Authorize] controller method I want to serialize object with all properties, even the one that excluded by [JsonIgnore]. Any idae?

DefaultModelBinder code not getting called for MVC Remote Validation Calls

I have a DefaultModelBinder which I have inherited from and I have tried overriding the BindModel call and OnPropertyValidating call.
However when using ASP.NET MVC's built in Remote validation, when the controller action gets called it bypasses my DefaultModelBinder and so it doesn't bind/validate how I want it to.
I have registered it in my global.asax, any ideas?

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.

How to stop Ninject from overriding custom DataAnnotationsModelValidatorProvider?

I have a custom DataAnnotationsModelValidatorProvider for doing model validation in a more dynamic way then just adding attributes. I tried to add my provide to the global.asax.cs like so:
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new AttributeValidatorProvider());
But once I load my form, I get an error saying "Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: required".
According to a comment on this blog, this is because Ninject is overriding custom validator providers.
I'm fairly new to MVC and I can't seem to find a way to tell Ninject to accept my custom providers as well, how would I go about fixing this problem?
For the record: I do not wish to use Fluentvalidation.net, I want to stick with the default MVC validations (for the most part).
There is another way (works in MVC 4 for sure):
Find your class which inherit IdependencyResolver interface and add to constructor _kernel.Unbind<ModelValidatorProvider>(); - you just unbind ninject validator and there should be no colission with default validator.
In my case my constructor looks like this:
public NinjectDependencyResolver()
{
_kernel = new StandardKernel();
_kernel.Unbind<ModelValidatorProvider>();
AddBindings();
}
Change the registration of the provider to
Rebind<ModelValidatorProvider>().To<AttributeValidatorProvider>();

Resources