ASP.NET 5, MVC6, WebAPI -> ModelState.IsValid always returns true

retsvek

I've seen a lot of posts about IsValid always being true but none of them have helped me solve this problem. I'm also seeing this problem in ASP.NET 4 using MVC5. So clearly I'm missing a step somewhere.

Controller method:

public IHttpActionResult Post([FromBody]ValuesObject value)
{
    if (ModelState.IsValid)
    {
        return Json(value);
    }
    else
    {
        return Json(ModelState);
    }
}

ValuesObject Class:

public class ValuesObject
{
    [Required]
    public string Name;

    [Range(10, 100, ErrorMessage = "This isn't right")]
    public int Age;
}

Body of the POST:

{
  Age: 1
}

ModelState.IsValid is true.

But I would expect both the Required and Range validations to fail.

What am I missing??

Thanks,

Kevin

Vadim Martynov

You can't use fields in your model. it's one of general conditions for your validation.

In ASP.NET Web API, you can use attributes from the System.ComponentModel.DataAnnotations namespace to set validation rules for properties on your model.

Replace it with properties and all will work fine:

public class ValuesObject
{
    [Required]
    public string Name { get; set; }

    [Range(10, 100, ErrorMessage = "This isn't right")]
    public int Age { get; set; }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET and EF Core - ModelState.IsValid always returns true for models generated by Scaffold-DbContext

ModelState.IsValid always returns false in ASP.NET Core 6.0 MVC

VB.NET - MVC ModelState.IsValid is always true

ASP.NET MVC 5 ModelState.IsValid is always returning false in MVC

ModelState IsValid returns False Using ASP.NET Core 5 MVC Identity with UserName

How ModelState.IsValid works in asp.net webapi

Why ModelState.IsValid always return true?

What is ModelState.IsValid valid for in ASP.NET MVC in NerdDinner?

Can I add WebApi to an ASP.NET 5 (vnext) project without MVC6?

ModelState.IsValid is always true while using Data annotation, MVC as Web API

ModelState.IsValid always evaluating as true when working with FluentValidation

ModelState.IsValid always true when accessed on View

ASP.NET 5 MVC6 User.GetUserId() returns incorrect id

Knockout validation plugin - isValid( ) always returns true

printDialog.PrinterSettings.IsValid returns true always

ModelState.IsValid includes a navigation property. Always false. (only net-6.0) How to get true?

ASP.NET MVC Core 3.0 - Why API Request from body keeps returning !ModelState.IsValid?

asp.net mvc2 save selected fileupload (when ModelState.IsValid = false)

ModelState.IsValid is false always in dotnet6 controller

ModelState.IsValid is true, but why?

MVC ModelState.IsValid=true with a null required property

ASP.NET 5 (MVC6) How to seed users

project.json in ASP.NET 5 and MVC6

Request BinaryRead in ASP.NET 5 (MVC6)

HTTP Error 403.14 in ASP.NET5 MVC6

@Ajax.ActionLink in ASP.NET 5 MVC6

Differentiate between MVC and WebAPI in ASP.NET 5 / MVC 6

ASP.NET MVC 5 "When ModelState is Invalid"

why does Powershell Test-Path -Isvalid always returns true

TOP Ranking

HotTag

Archive