I am attempting to perform client side validation on my objects that come back through WebApi. I send the entity through webapi to my Entity edit screen. I use knockout to bind the object to the fields.
I already have an action filter handling all the server side validation. How could I incorporate the client side validation without having to duplicate my domain model data annotations?
With WebApi, you need a little “glue” code to connect the errors coming back from model validation failures to the client side validator.
On the model, annotate as usual:
In the view, add the ValidationMessageFor tags:
When I create my HTTP API, I put the model objects (DTOs, request models, etc) into a separate assembly which I can distribute for .NET clients.
Consider the following class:
This is what I use in my API:
You can use this same model in your ASP.NET MVC client application for example to generate the HTML inputs with validation
data-
attributes as ASP.NET MVC has a way of generating those based on data annotation validation attributes.