Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name 'ModelState' does not exist in the current context

If we look at the manual here it states that we should use same old ModelState.IsValid or TryValidateModel() yet when I create a project, restore it I don't have access to it.

What am I doing wrong?

Here is the manual: https://docs.asp.net/en/latest/tutorials/first-mvc-app/validation.html

The name 'ModelState' does not exist in the current context

using Api.Models;
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
{
    [Route("api/authorization")]
    public class AuthorizationController
    {
        [Route("login"), HttpPost]
        public IActionResult Authorize(UserViewModel model)
        {
            if (ModelState) // ModelState does not exist.
            {
            }

            return null;
        }
    }
}
like image 682
Stan Avatar asked Aug 29 '16 16:08

Stan


1 Answers

Your controller needs to inherit from ApiController (or Controller). What you have there isn't really a controller!

like image 87
Andiih Avatar answered Oct 12 '22 23:10

Andiih