Validation: How to inject A Model State wrapper with Ninject?

The solution given by that article mixes validation logic with the service logic. These are two concerns and they should be separated. When your application grows you will quickly find out that validation logic gets complicated and gets duplicated throughout the service layer. I, therefore, like to suggest a different approach. First of all, it … Read more

ModelState.IsValid is always returning false [duplicate]

ModelState.IsValid will be false if the validation for the Model failed. You have DataAnnotation which failed the incoming model. You added custom validations. Make sure there are no null entries in the model for non null properties Check the ModelState.Errors for what is the reason causing this. You can use this: var errors = ModelState.Values.SelectMany(v … Read more

What does ModelState.IsValid do?

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. In your example, the model that is being bound is of class type Encaissement. Validation rules are those specified on the model by … Read more

ASP.NET MVC Controller post method unit test: ModelState.IsValid always true

You’re trying to test two different things at the same time. The controller is not reponsible for validating the model state, only for behaving differently based on the result of that validation. So your unit tests for the controller shouldn’t try to test the validation, that should be done in a different test. In my … Read more

ASP.NET MVC – How to Preserve ModelState Errors Across RedirectToAction?

I had to solve this problem today myself, and came across this question. Some of the answers are useful (using TempData), but don’t really answer the question at hand. The best advice I found was on this blog post: http://www.jefclaes.be/2012/06/persisting-model-state-when-using-prg.html Basically, use TempData to save and restore the ModelState object. However, it’s a lot cleaner … Read more

ASP.NET MVC How to convert ModelState errors to json

You can put anything you want to inside the select clause: var errorList = (from item in ModelState where item.Value.Errors.Any() select item.Value.Errors[0].ErrorMessage).ToList(); EDIT: You can extract multiple errors into separate list items by adding a from clause, like this: var errorList = (from item in ModelState.Values from error in item.Errors select error.ErrorMessage).ToList(); Or: var errorList … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)