Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call model class in controller class

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

like image 981
Striker Avatar asked May 23 '14 11:05

Striker


1 Answers

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();
}
like image 143
Ehsan Sajjad Avatar answered Oct 19 '22 18:10

Ehsan Sajjad