Questions tagged [model-validation]

Model Validation is ASP.NET MVC validation method for your models with property attributes. Model validation works for client and server side validation.

For further information you can check Scott Gu's blog post: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

There is a expansion called "DATA ANNOTATIONS EXTENSIONS" for other custom validation types such as CreditCard, min-max integer value etc. http://weblogs.asp.net/srkirkland/archive/2011/02/23/introducing-data-annotations-extensions.aspx

325 questions
5
votes
1 answer

ASP.NET MVC Model Binding and Validation Order

I have a model (simplified) as follows: public UserModel { ... public USState State {get; set; } public string StateString {get; set; } public Country Country {get; set; } ... } The validation rules I need are: If Country is…
5
votes
1 answer

How To Add Custom ModelValidatorProviders To Web API Project?

I'm moving some MVC code to Web API and I need to update my custom ModelValidatorProviders. It seems as though my validators can stay much the same only they should inherit the System.Web.Http.Validation namespace. What I can't figure out is how to…
Mark
  • 21,067
  • 14
  • 53
  • 71
5
votes
1 answer

Server side validation with custom DataAnnotationsModelValidatorProvider

I have setup a custom provider to allow setting validation attributes from a data store instead of in static code. Works great with the client side validation in my .NET MVC 4 project, but I am unable to get the server side validation to…
4
votes
1 answer

How can I create a "decorator abbreviation" in ASP.net Core

I am working on a project with a lot of models where I need the same validation with localized error messages happen at a couple of different places. Right now I decorate the corresponding properties with [RegularExpression(Bla,…
Mark
  • 39
  • 5
4
votes
2 answers

Model Validation .Net Core 2.0 Web Api Not working

I have a .Net Core 2.0 Web Api. I have various models with validation attributes on properties as such: [Required] public short? Quantity { get; set; } I have an ActionFilter that checks model state: if (!context.ModelState.IsValid) …
4
votes
1 answer

Checkit with bookshelf, how to catch error and stop DB execution?

I'm trying to use checkit with bookshelf and after adding the checkit rules, intentionally violating them, my promise#catch block doesn't seem to be properly catching the errors. (I could also be totally misunderstanding the use of catch here) var…
bahrieinn
  • 343
  • 2
  • 11
4
votes
3 answers

Auto generating metadata classes for Entity Framework

I am considering using xVal for validation of Entity Framework classes in a MVC application. This involves writing metadata classes as explained in details by Graham O'Neale…
K.A.D.
  • 3,648
  • 3
  • 34
  • 35
4
votes
1 answer

POST request without content pass model validation

I have an ASP.NET application using Web API 2. To force model validation on all actions, I use a filter, like so: public class ValidateModelAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext…
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
4
votes
2 answers

ASP.NET MVC Model Validation Error Localization Context

First of all, I have to say that I understand how Data Annotation -based Model Validation works in ASP.NET MVC4 and I have it successfully implemented with DataAnnotationsModelValidatorProvider. So I don't need assistance on setting it up. But when…
4
votes
1 answer

ASP .NET MVC 4 View containing two partial Views with a form in each

In my MVC 4 application I have an Index View. In this View I have two div tags where a Partial view is rendered. There are two buttons that when clicked toggles between these two divs using jQuery. The Partial Views are strongly typed with its model…
4
votes
3 answers

MVC3 Client-side validation on collection, at least one element with value

My ViewModel Public Class ViewModel Public Property Collection As List(Of Item) End Class My Model Public Class Item Public Property Selected As…
3
votes
1 answer

"user may do X is user owns object Y": Implement logic in Model Validation or Controller logic?

Consider, for example's sake, the logic "A user may only edit or delete a comment that the user has authored". My Controller Actions will repeat the logic of checking whether the currently logged in user can affect the comment.…
jdgilday
  • 866
  • 7
  • 21
3
votes
2 answers

How to get model validation to pickup attributes set on objects in a List in MVC3?

I have a set of models that looks similar to this public class OtherModel { [Required] string name { get; set; } } public class OthersEditModel { List others { get; set; } } I then have a controller method that looks like…
Josh Russo
  • 3,080
  • 2
  • 41
  • 62
3
votes
1 answer

Moving Model Validation to Service Class - ASP.NET MVC

I want to be able to perform validation from within my Service classes. I have a Controller action that looks something like this: public ActionResult Edit(Post post) { if(!ModelState.IsValid) return View(); …
enamrik
  • 2,292
  • 2
  • 27
  • 42
3
votes
2 answers

What is the best way to identify that a model binding has failed in ASP.NET Core 6.0+

I have gone through the MSDN documentation: No source for a model property Type conversion errors I tried creating a scenario where value sent from the swagger to the API, failed to bind to the model, that is expected on the server. Here is the…
1 2
3
21 22