5.1. Problem

Now that we have our membership working, users can sign up, log in, log out, and reset their passwords. Before we start delving into the rest of the application, we need to determine our validation strategy and make sure it works with the rest of the application before we go too far and create a bunch of views.

So far, we have implemented server validation using if statements and the model state to indicate errors. Functionally this works well, but we need to do better. Ideally, we want to validate on both the client and the server. Client validation allows us to give users instant feedback without the overhead of a server round-trip. Server validation ensures that the data is valid even if client validation fails or the client doesn't support JavaScript or has it disabled. We also need an easy way to define validation rules, preferably on the model itself. Lastly, we need to get the validation out of the controller and into the service (business logic) layer, so that our validation logic is not tightly coupled to the controller.

To summarize, we need to:

  • Validate on the client.

  • Validate on the server.

  • Define validation rules on the model.

  • Perform validation in the service (business logic) layer.

Get ASP.NET MVC 1.0 Test Driven Development: Problem - Design - Solution now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.