Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Fluent API configuration work with MVC client side validation?

I prefer working with Fluent API configuration to DataAnnotation because I want to separate model from data access.

I have tried in MVC, Fluent API does not work with client side validation. Shortly speaking, is there a simple way to make Fluent API works with client side validation as DataAnnotation can do?

like image 255
kiss my armpit Avatar asked Jan 17 '12 12:01

kiss my armpit


People also ask

Is data annotation server side validation in MVC?

In ASP.NET MVC application we can do the Server Side Validation on the Model using Data Annotations. Data Annotations is a namespace that provides attribute classes, and these classes define metadata for controls. In MVC we decorate Model Properties with these attribute classes to provide metadata.

Which method is used to enable/disable client-side validation in MVC?

To disable client-side validation, set the page's ClientTarget property to 'Downlevel' ('Uplevel' forces client-side validation). Alternatively, you can set an individual validator control's EnableClientScript property to 'false' to disable client-side validation for that specific control.

How client-side validation is implemented in MVC?

Firstly, you just need to create an ASP.NET MVC application. To create a new ASP.NET MVC application, Open Visual Studio choose File, New, then Project. It will open a New Project window, from where you need to choose node Visual C# and then Web and from the right pane you need to choose ASP.NET Web Application.


2 Answers

No. Fluent API is just mapping - correct. Data annotations are both mapping and validation - wrong. Data annotations are one of the worst features of EF code first because when used this way they couple persistence with presentation and validation logic.

Advice: don't use EF entities for presentation. Use special view models with data annotations and let your controller prepare view models from entities and vice-versa. Soon or later you will find situations where your validation is not 1:1 with your mapping or where your view needs more or less data than provided in entity type. Use view models and these situations will be handled by them.

like image 175
Ladislav Mrnka Avatar answered Sep 24 '22 17:09

Ladislav Mrnka


I struggled with this for a while today, and this is not strictly client validation as it requires a round trip, but it does allow you to benefit from the Validation summary and messages helpers in the standard template. Within your controller action method you simply wrap your SaveChanges() call in a try - catch and add the resulting Errors to the ModelState as follows:

try {      //This does not pick up fluent validation failures     if (ModelState.IsValid) {         db.Entity.Add(entity);         db.SaveChanges();         //Users want to create loads of my entities without seeing the index...         return RedirectToAction("Create");     }  } catch (DbEntityValidationException e) {      //Log errors     foreach (var result in e.EntityValidationErrors) {         foreach(var error in result.ValidationErrors){             ModelState.AddModelError(error.PropertyName, error.ErrorMessage);         }     }  }  //return to view with current model + validation errors  return View(entity) 

This would of course require a bit more work if you are saving multiple entities here.

Of course using a View Model objects as Ladislav suggests would be the correct approach, however I have used this to support a test UI requested for downstream systems integration testing ahead of schedule...

like image 31
kas Avatar answered Sep 22 '22 17:09

kas