how to call model class in controller class

27,056

You have not included namespace of Models.

Just Right Click on Users class name in the controller and go to Resolve and from their include its namespace in controller class.

If you want to do explicitly then, your project namespace assming MyProject, do like this:

using MyProject.Models;


public class UserController : Controller
{
   private static Users _users = new Users();
}

or you can use Fully Qualified Name like this:

public class UserController : Controller
{
  private static MyProject.Models.Users _users = new MyProject.Models.Users();
}
Share:
27,056
Striker
Author by

Striker

I'm a complete software engineer who has solutions of all your IT problems. I love to develop things. Websites to webs services to mobile application to API's. Front end to back end. Your problem my solution.

Updated on May 24, 2020

Comments

  • Striker
    Striker almost 4 years

    mvc4

    this is my controller class in controller folder

    public class UserController : Controller
    {
        //
        // GET: /User/
        private static Users _users = new Users();
        public ActionResult Index()
        {
            return View(_users._userList);
        }
    
        public ActionResult UserAdd()
        {
            return View();
        }
        [HttpPost]
        public ActionResult UserAdd(UserModels userModel)
        {
            _users.CreateUser(userModel);
            return View();
        }
    
    }
    

    Error 2 The type or namespace name 'Users' could not be found

    Error 3 The type or namespace name 'UserModels' could not be found

    this is my user class in model folder

     public class Users
        {
            public Users()
            {
                _userList.Add(new UserModels
                {
                    FirstName = "birbal ",
                    LastName = "kumar",
                    Address = "new delhi",
                    Email = "[email protected]",
                    DOB = Convert.ToDateTime("2/11/1991"),
                    salary = 8000
                });
             }
        }
    

    this is my usermodel class in model folder

    public class UserModels
    {
        [DisplayName("First Name")]
        [Required(ErrorMessage="First name is required")]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        public string Address { get; set; }
        [Required()]
        [StringLength(50)]
        public string Email { get; set; }
        [DataType(DataType.Date)]
        public DateTime DOB { get; set;  }
        [Range(100,1000000)] 
        public decimal salary { get; set; }
    }
    

    how to remove my errors