ASP.NET Core 6.0 MVC Tutorial Day 6
Learning Objective: Let’s crate login page and later on connect with database to verify user is valid or not.
Home Controller Code
using Microsoft.AspNetCore.Mvc;
namespace WebApplication1.Areas.Admin.Controllers
{
[Area("Admin")]
public class HomeController : Controller
{
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(string username, string password)
{
ViewBag.username = username;
ViewBag.password = password;
return View("Result");
}
}
}
Here I am create two methods, 1 method to display login page (form) and on submit i am redirecting user data to another page using post method. Here I have specified method with Get and Post options so based on Get/Post appropriate method will be called.
Get Method
Get method will just display my view with form.
Post Method
Post method will collect username and password from the previous page and will store data into viewbag and later on it will pass data to view. Currently in view i am displaying data on view, you can do the verification with database easily.
Index View (for displaying form)
<form method="post">
<label for="username">Username</label>
<input placeholder="" type="text" id="username" name="username" value="">
<br />
<label for="password">Password</label>
<input placeholder="" type="password" id="password" name="password" value="">
<br />
<input type="submit">
</form>
Result View (to display fetched value from form)
Entered Username is : @ViewBag.username
<br />
Entered Password is : @ViewBag.password