1
votes

HomeController

    [HttpGet]
    public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Login(Login l)
    {
        if (ModelState.IsValid)
        {
            if (l.CheckUser(l.UserName, l.Password))
            {
                return RedirectToAction("Home/Home");
            }
            else
            {
                Response.Write("Invalid User");
                return View();
            }
        }
        else
        {
            return View();
        }

    }

Model class (Login)

 namespace sampleprojectone.Models
 {
    public class Login
    {
        [Required(ErrorMessage ="Username must not be empty")]
        [Display(Name ="Username")]
        public string Username { get; set; }
        [Required(ErrorMessage = "Password must not be empty")]
        [Display(Name = "Password")]
        public string Password { get; set; }
        public bool CheckUser(string username,string password)
           {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ToString());
                con.Open();
                SqlCommand cmd = new SqlCommand("proc_checklogin", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@password", password);
                bool b = Convert.ToBoolean (cmd.ExecuteScalar());
                return b;
           }
     }
 }

Error

Severity Code Description Project File Line Suppression State Error CS1061 'Login' does not contain a definition for 'CheckUser' and no accessible extension method 'CheckUser' accepting a first argument of type 'Login' could be found (are you missing a using directive or an assembly reference?) sampleprojectone G:\King\Projects\sampleprojectone\sampleprojectone\Controllers\HomeController.cs 35 Active

1
I'm uncertain, but I think what it might be that you have not referenced your Login class properly yet. Try to have your Login functions named differently than your login class... public ActionResult Login() --> public ActionResult LoginAction() public ActionResult Login(Login l) --> public ActionResult LoginWithCreds('Login l') And try again. If your Login model can not be found, it might be that you have not referenced it properlyCornelis
Is it possible that you have a Login controller and as such you need to fully qualify the Login object in your Login method? public ActionResult Login(sampleprojectone.Models.Login l)Rob

1 Answers

0
votes

I haven't used namespace using sampleprojectone.Models; in HomeController. Now it is solved thank you cornelis and rob.