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
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 properly – Cornelis