Advertisement

Advertisement

Asp.net Mvc Login Page

 Asp.net Mvc Login Page

Tbl_RegisterController.cs

public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(Tbl_Register u)
        {
            var user = db.Tbl_Register.Where(x => x.Username ==  u.Username && x.Password == u.Password).Count();
            if (user != null)
            {
                Session["UserId"] = u.Id.ToString();
                Session["UserName"] = u.Username.ToString();
                TempData["LoginSucessMessage"] = "<script>alert('Login Success')</script>";
                return RedirectToAction("Index", "Tbl_Register");
            }
            else
            {
                ViewBag.ErrorMessage = "<script>alert('Username or Password incorrect')</script>";
                return View();
            }
            
        }

Login.cshtml

@model MvcHTp.Models.Tbl_Register

@{
    ViewBag.Title = "Login";
}

<h2>Login</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Login Here</h4>
        @Html.Raw(TempData["LoginSucessMessage"])
        @Html.Raw(ViewBag.ErrorMessage)
        <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.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
            </div>
        </div><div class="form-group">
            @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ConfirmPassword, "", 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>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Asp.net Mvc Login Page Asp.net Mvc Login Page Reviewed by Rikesh on June 09, 2023 Rating: 5

No comments:

Powered by Blogger.