Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC ModelState is always valid with Fluent Validation

I am trying to use fluent validation with ASP.NET MVC project. I am trying to validate my view model.

This is my viewmodel,

[Validator(typeof(ProductCreateValidator))]
public class ProductCreate
{
    public string ProductCategory   { get; set; }
    public string ProductName       { get; set; }
    ....
}

This is my validator class,

public class ProductCreateValidator : AbstractValidator<ProductCreate> 
{
    public ProductCreateValidator()
    {
        RuleFor(product => product.ProductCategory).NotNull();
        RuleFor(product => product.ProductName).NotNull();
    }
}

And in my controller, I am checking whether my ModelState is valid or not,

[HttpPost]
public ActionResult Create(ProductCreate model)
{
    /* This is a method in viewmodel that fills dropdownlists from db */
    model.FillDropDownLists();

    /* Here this is always valid */
    if (ModelState.IsValid)
    {
        SaveProduct(model);
        return RedirectToAction("Index");
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

This is what I have. My problem is ModelState.IsValid returns true when my viewmodel is completely empty. Do i need to manually configure Fluent validation so that model errors can be added to ModalState ?

like image 392
emre nevayeshirazi Avatar asked Jul 26 '12 11:07

emre nevayeshirazi


People also ask

What is ModelState valid?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process.

How does ModelState work in MVC?

In short, the ModelState is a collection of name and value pairs that are submitted to the server during a POST. It also contains error messages about each name-value pair, if any are found. ModelState is a property of a Controller instance, and can be accessed from any class that inherits from Microsoft. AspNetCore.

What is the use of ModelState IsValid validate?

ModelState. IsValid property can be used to perform some logical operations based on the values submitted by the User. Note: If you want to learn about Client Side validations in ASP.Net MVC Razor, please refer ASP.Net MVC: Client Side validations using Data Annotation attributes and jQuery.

What is the use of ModelState clear in MVC?

Clear() is required to display back your model object. If you are getting your Model from a form and you want to manipulate the data that came from the client form and write it back to a view, you need to call ModelState. Clear() to clean the ModelState values.


1 Answers

As the documentation explains, make sure you have added the following line in your Application_Start in order to swap the data annotations model metadata provider and use fluent validation instead:

FluentValidationModelValidatorProvider.Configure();

Also the following comment in your action scares me:

/* This is a method in viewmodel that fills dropdownlists from db */
model.FillDropDownLists();

A View model shouldn't know what a database means. So having such methods in your view model is a very wrong approach.

like image 55
Darin Dimitrov Avatar answered Sep 28 '22 04:09

Darin Dimitrov