Hi rani,
Check this example. Now please take its reference and correct your code.
I have used table Users having columns UserId, Username, Password, CreatedDate.
Model
public class LoginViewModel
{
public string user_name { get; set; }
public string password { get; set; }
public User user { get; set; }
}
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public DateTime CreatedDate { get; set; }
}
Controller
public class HomeController : Controller
{
private readonly IConfiguration _configuration;
public HomeController(IConfiguration configuration)
{
this._configuration = configuration;
}
public IActionResult Index()
{
return View();
}
public IActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Login(LoginViewModel loginViewModel)
{
using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_mvc")))
{
string query = "SELECT UserId,Username,CreatedDate FROM Users WHERE Username = @user_name AND Password = @password";
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@user_name", loginViewModel.user_name);
cmd.Parameters.AddWithValue("@password", loginViewModel.password);
DataTable dt = new DataTable();
con.Open();
dt.Load(cmd.ExecuteReader());
con.Close();
if (dt.Rows.Count > 0)
{
//Message if login succesfuly.
ViewBag.Message = "Successfully Login";
ViewBag.User = new User
{
UserId = Convert.ToInt32(dt.Rows[0]["UserId"]),
Username = dt.Rows[0]["Username"].ToString(),
CreatedDate = Convert.ToDateTime(dt.Rows[0]["CreatedDate"])
};
return View(loginViewModel);
}
else
{
//Message if login failed.
ViewBag.Message = "Login Failed. Wrong Username or Password !!!";
return View(loginViewModel);
}
}
}
}
View
Index
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<a href="/Home/Login">Login</a>
</body>
</html>
Login
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model Login_Message_MVC_Core.Models.LoginViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<link href="~/lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
</head>
<body class="hold-transition login-page container">
<div class="login-box">
<div class="login-logo">
<a href=""><b></b></a>
</div>
<div class="card">
<div class="card-body login-card-body">
<h2 class="text-center">
<b>Admin Login</b>
</h2>
<h5></h5>
<form asp-action="Login" method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="input-group mb-3">
<input asp-for="user_name" class="form-control" placeholder="User Name" required>
<span asp-validation-for="user_name" class="text-danger"></span>
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-envelope"></span>
</div>
</div>
</div>
<div> </div>
<div class="input-group mb-3">
<input type="password" asp-for="password" class="form-control" placeholder="Password" required>
<span asp-validation-for="password" class="text-danger"></span>
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-lock"></span>
</div>
</div>
</div>
<div> </div>
<div class="row">
<div class="col-sm-2">
<button type="Submit" class="btn btn-success btn-block">Sign In</button>
</div>
</div>
</form>
@if (ViewBag.User != null)
{
<table class="table">
<tr>
<td>Id</td>
<td>@ViewBag.User.UserId</td>
</tr>
<tr>
<td>Name</td>
<td>@ViewBag.User.Username</td>
</tr>
<tr>
<td>Created</td>
<td>@ViewBag.User.CreatedDate.ToString("dd/MM/yyyy")</td>
</tr>
</table>
}
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewBag.Message");
};
</script>
}
</div>
</div>
</div>
</body>
</html>
Screenshot