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
8
votes
1 answer

How to perform async ModelState validation with FluentValidation in Web API?

I setup a web api project to use FluentValidation using the webapi integration package for FluentValidation. Then I created a validator that uses CustomAsync(...) to run queries against the database. The issue is that the validation seems to…
Natan
  • 4,686
  • 5
  • 30
  • 48
7
votes
3 answers

How to avoid ViewBag (or ViewData) in favor of a Model?

This is a very simple example, but it should be enough to demonstrate my issue. I need to pass a model to my view that the user will update, but the view also needs some other data to create a dropdownlist or to provide other information. Based on…
JustinStolle
  • 4,182
  • 3
  • 37
  • 48
7
votes
2 answers

How to discern between model binding errors and model validation errors?

I'm implementing a REST API project using ASP.NET Core MVC 2.0, and I'd like to return a 400 status code if model binding failed (because the request is syntactically wrong) and a 422 status code if model validation failed (because the request is…
Albireo
  • 10,977
  • 13
  • 62
  • 96
7
votes
1 answer

FluentValidation Doesn't Work When Using WebApi [Route] Attribute

I successfully implemented FluentValidation in my WebApi project controller that only had one HttpGet method. When I added another HttpGet method, I added route attribute to both methods. i.e. [Route("Method1")] and [Route("Method2")]. Now the…
John Way
  • 227
  • 1
  • 5
  • 17
6
votes
3 answers

ASP.NET MVC model validation breaks MVC rule?

using ASP.NET MVC, I have a Model, to which I'm attaching attributes so that I can get use the MVC Model bound validation, but to doesn't this break the rule of MVC, where you are putting items that belong to the View, into the Model? I hope I don't…
Tomasz Iniewicz
  • 4,379
  • 6
  • 42
  • 47
6
votes
2 answers

Validate multiple Partial view without BeginForm in a View

I have a View (Index.cshtml) that it has two modals (Bootstrap modal). I have loaded a Partial View in each ‍‍‍modal. So in this View, I have two Partial Views(AddContractHistory.cshtml and AddCompany.cshtml). I have a model that it's fields should…
Reza Amini
  • 476
  • 1
  • 5
  • 20
6
votes
1 answer

django manytomany validation

Please see the code below. Basically, when the user creates an object of this class, they need to specify the value_type. If value_type==2 (percentage), then percentage_calculated_on (which is a CheckboxSelectMultiple on the form/template side needs…
chefsmart
  • 6,873
  • 9
  • 42
  • 47
6
votes
4 answers

How to use Html.CheckBox (list) with IEnumerable with validation

I'm working on a page where the user needs to fill in some information and finally make a selection of 1 or more customers with check-boxes. The list of customers is an IEnumerable which I pass into my Model. How would I go about creating…
Fabian
  • 13,603
  • 6
  • 31
  • 53
6
votes
2 answers

Exclude Fields From Model Validation

Let's say I have a following ViewModel : public class PersonViewModel { [Required] public String Email { get; set; } [Required] public String FirstName { get; set; } [Required] public String…
Saber Amani
  • 6,409
  • 12
  • 53
  • 88
6
votes
4 answers

How to disable MVC 4 model validation?

I am working on a project written on mvc 4 that got several instances of wizard-like behaviour - chain of few views that pass the same half-filled model. Starting from the second view controls are initially shown as non-valid (which is logical -…
user1782982
  • 75
  • 2
  • 9
6
votes
3 answers

MVC DateTime validation failing

I found a lot of simulair questions but not a good clean solution that is working. I see a lot of custom code for getting this to work but why is that? Should this not working from the start? What I think is strange, is that in IE9 it works but in…
5
votes
1 answer

Custom Model Validator for Integer value in ASP.NET Core Web API

I have developed a custom validator Attribute class for checking Integer values in my model classes. But the problem is this class is not working. I have debugged my code but the breakpoint is not hit during debugging the code. Here is my code:…
mnu-nasir
  • 1,642
  • 5
  • 30
  • 62
5
votes
1 answer

Required string attribute with null value gives IsValid=true in ASP.NET Core 2 Razor Page

I'm really confused by this. I have a Razor Page on ASP.NET Core 2 that has a required property called SchemaId. I've tried marking it as [Required], [BindRequired], and [Required(AllowEmptyStrings = false)], yet when I post my form, I see that…
Seafish
  • 2,081
  • 2
  • 24
  • 41
5
votes
3 answers

Is there a way to validate a model which is created inside web api controller?

I have a controller where my PUT method uses multipart/form-data as content type and so I am getting the JSON and the mapped class thereby inside the controller. Is there a way I could validate this model with respect to the annotations I have…
Jose Francis
  • 950
  • 13
  • 28
5
votes
3 answers

Is it possible to use Web API model validation on query parameters?

I am currently trying to write a Web API application where one of the parameters I'd like to validate is a query parameter (that is, I wish to pass it in in the form /route?offset=0&limit=100): [HttpGet] public async Task
coppro
  • 14,338
  • 5
  • 58
  • 73
1
2
3
21 22