Welcome to ASP.NET MVC Tutorial on How to make Login System.
Just Put this code in home Controller.
public ActionResult login()
{
return View();
}
[HttpPost]
public ActionResult login(user user)
{
StudentContext db = new StudentContext();
var userLoggedIn = db.users.SingleOrDefault(x => x.username == user.username && x.password == user.password);
if (userLoggedIn != null)
{
ViewBag.message = "loggedin";
ViewBag.triedOnce = "yes";
return RedirectToAction("mainpage", "home",new { username= user.username });
}
else
{
ViewBag.triedOnce = "yes";
return View();
}
}
And Paste this code in Home Folder's Login.cshtml
@model ASPtutorial.Models.user
@{
ViewBag.Title = "login";
}
<h2>login</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
if (ViewBag.message == null && ViewBag.triedOnce == "yes")
{
<h4>Username or Password not matched, please log in again or register.@Html.ActionLink("Register", "Register")</h4>
}
<div class="form-horizontal">
<h4>user</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.username, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.username, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.username, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Just Put this code in home Controller.
public ActionResult login()
{
return View();
}
[HttpPost]
public ActionResult login(user user)
{
StudentContext db = new StudentContext();
var userLoggedIn = db.users.SingleOrDefault(x => x.username == user.username && x.password == user.password);
if (userLoggedIn != null)
{
ViewBag.message = "loggedin";
ViewBag.triedOnce = "yes";
return RedirectToAction("mainpage", "home",new { username= user.username });
}
else
{
ViewBag.triedOnce = "yes";
return View();
}
}
And Paste this code in Home Folder's Login.cshtml
@model ASPtutorial.Models.user
@{
ViewBag.Title = "login";
}
<h2>login</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
if (ViewBag.message == null && ViewBag.triedOnce == "yes")
{
<h4>Username or Password not matched, please log in again or register.@Html.ActionLink("Register", "Register")</h4>
}
<div class="form-horizontal">
<h4>user</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.username, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.username, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.username, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
For more questions or queries ask me on fb.com/ythecoder or you can watch this video too. If you are interested in online class then do contact me in affordable price. :)
No comments:
Post a Comment