Online Restaurant Website Part 7 User Setting

  Online Restaurant Website Part 7 User Setting

Hi, Dear's here we learn how to implement Restaurant Website in Visual Studio using C# ASP.NET MVC. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like for more project base tutorials




In this video we are going to User setting in asp.net mvc form online restaurant for more details click here: watch video

Next add empty UserController in controller folder then add the below code for UserTable to add user, login user.

 > LoginMV Code : 

using System.ComponentModel.DataAnnotations;

namespace PizzaRestaurantDrink.Models
{
    public class LoginMV
    {
        [Required(ErrorMessage ="Required*")]
        [DataType(DataType.Text)]
        public string UserName { get; set; }
        [Required(ErrorMessage ="Password")]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
}

So Now Add Login code to User Controller.

> Login Action Code :  
   add below code. 

using DBLayer;
using PizzaRestaurantDrink.Models;
using System;
using System.Linq;
using System.Web.Mvc;

namespace PizzaRestaurantDrink.Controllers
{
    public class UserController : Controller
    {
        PizzaRestaurentandDrinksDbEntities Db = new PizzaRestaurentandDrinksDbEntities();
        public ActionResult Login()
        {
            var user = new LoginMV();
            return View(user);
        }

        [HttpPost]
        public ActionResult Login(LoginMV loginMV)
        {

            if (ModelState.IsValid)
            {
                var user = Db.UserTables.Where(u => u.UserName.Trim() == loginMV.UserName.Trim() && u.Password.Trim() == loginMV.Password.Trim()).FirstOrDefault();
                if (user != null)
                {
                    if (user.UserStatusID == 1)
                    {
                        Session["UserID"] = user.UserID;
                        Session["UserTypeID"] = user.UserTypeID;
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        var accountstatus = Db.UserStatusTables.Find(user.UserStatusID);
                        ModelState.AddModelError(string.Empty, "Account is " + accountstatus.UserStatus + "");
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Please Enter Correct User Name and Password!");
                }
            }
            Session["UserID"] = string.Empty;
            Session["UserTypeID"] = string.Empty;
            return View(loginMV);
        }
        public ActionResult Logout()
        {
            Session["UserID"] = string.Empty;
            Session["UserTypeID"] = string.Empty;
            return RedirectToAction("Index","Home");
        }
    }
}

 > Login View Code : 

@model PizzaRestaurantDrink.Models.LoginMV
@{
    ViewBag.Title = "Login";
}

<!-- Breadcrumb Start -->
<div class="bread-crumb">
    <div class="container">
        <div class="matter">
            <h2>Login</h2>
            <ul class="list-inline">
                <li class="list-inline-item"><a href="@Url.Content("~/Home/Index")">HOME</a></li>
                <li class="list-inline-item"><a href="@Url.Content("~/User/Login")">Login</a></li>
            </ul>
        </div>
    </div>
</div>
<!-- Breadcrumb End -->
<!-- Login Start -->
<div class="login">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-sm-12 commontop text-center">
                <h4>Login to Your Account</h4>
                <div class="divider style-1 center">
                    <span class="hr-simple left"></span>
                    <i class="icofont icofont-ui-press hr-icon"></i>
                    <span class="hr-simple right"></span>
                </div>
            </div>
            <div class="col-lg-10 col-md-12">
                <div class="row">
                    <div class="col-sm-12 col-md-6">
                        <div class="loginnow">
                            <h5>Login</h5>
                            <p>Don't have an account? So <a href="@Url.Content("~/User/Register")">Register</a> And starts less than a minute</p>
                            @using (Html.BeginForm("Login", "User", FormMethod.Post, new { @enctype = "multipart/form-data" }))
                            {
                                @Html.AntiForgeryToken()
                                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                                <div class="form-group">
                                    <i class="icofont icofont-ui-user"></i>
                                    @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter User Name" } })
                                    @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
                                </div>
                                <div class="form-group">
                                    <i class="icofont icofont-lock"></i>
                                    @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Password" } })
                                    @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                                </div>
                                <div class="form-group">
                                    <div class="links">
                                        <label><input type="checkbox" class="radio-inline" />Remember Me</label> <a class="float-right sign" href="forgot-password.html">Forgot Password?</a>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <input type="submit" value="SIGN IN" class="btn btn-theme btn-md btn-wide" />
                                </div>
                            }
                        </div>
                    </div>

                    <div class="col-sm-12 col-md-6">
                        <div class="loginto loginnow">
                            <h5>Login via social accounts</h5>
                            <ul class="list-unstyled text-center">
                                <li><a href="#" target="_blank"><i class="icofont icofont-social-facebook"></i> Login with Facebook</a></li>
                                <li><a href="#" target="_blank"><i class="icofont icofont-social-google-plus"></i> Login with Google+</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Login End -->

Next we are going to register user


> Reg_UserMV Code : 

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace PizzaRestaurantDrink.Models
{
    public class Reg_UserMV
    {
        public int UserID { get; set; }

        [Required(ErrorMessage="Required*")]
        public int UserTypeID { get; set; }

        [Required(ErrorMessage = "Required*")]
        [DataType(DataType.Text)]
        [Display(Name ="User Name")]
        public string UserName { get; set; }

        [Required(ErrorMessage = "Required*")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [Required(ErrorMessage = "Required*")]
        [DataType(DataType.Password)]
        [Compare("Password", ErrorMessage = "Not Match!")]
        public string ConfirmPassword { get; set; }

        [Required(ErrorMessage = "Required*")]
        [DataType(DataType.Text)]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        public string LastName { get; set; }

        [Required(ErrorMessage = "Required*")]
        [DataType(DataType.PhoneNumber)]
        [Display(Name = "Contact No")]
        public string ContactNo { get; set; }

        [Required(ErrorMessage = "Required*")]
        [Display(Name = "Select Gender")]
        public int GenderID { get; set; }

        [Required(ErrorMessage = "Required*")]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email Address")]
        public string EmailAddress { get; set; }
        public System.DateTime RegisterationDate { get; set; }
        public int UserStatusID { get; set; }
        public Nullable<System.DateTime> UserStatusChangeDate { get; set; }
    }
}

So Now Add User Register code to User Controller.

> Register Action Code :  
   add below code. 

public ActionResult Register()
        {
            var user = new Reg_UserMV();
            ViewBag.GenderID = new SelectList(Db.GenderTables.ToList(), "GenderID", "GenderTitle", "0");
            return View(user);
        }

        [HttpPost]
        public ActionResult Register(Reg_UserMV reg_UserMV)
        {
            reg_UserMV.UserTypeID = 4; // Customer User_Type_ID
            reg_UserMV.RegisterationDate = DateTime.Now;
            reg_UserMV.UserStatusID = 1;
            if (ModelState.IsValid)
            {
                bool isexist = false;
                var checkexist = Db.UserTables.Where(u => u.UserName.ToUpper().Trim() == reg_UserMV.UserName.ToUpper().Trim()).FirstOrDefault();
                if (checkexist != null)
                {
                    isexist = true;
                    ModelState.AddModelError("UserName", "Already Exist!");
                }
                checkexist = Db.UserTables.Where(u => u.EmailAddress.ToUpper().Trim() == reg_UserMV.EmailAddress.ToUpper().Trim()).FirstOrDefault();
                if (checkexist != null)
                {
                    isexist = true;
                    ModelState.AddModelError("EmailAddress", "Already Exist!");
                }
                if (isexist == false)
                {
                    var user = new UserTable();
                    user.UserTypeID = reg_UserMV.UserTypeID;
                    user.UserName = reg_UserMV.UserName;
                    user.Password = reg_UserMV.Password;
                    user.FirstName = reg_UserMV.FirstName;
                    user.LastName = reg_UserMV.LastName;
                    user.ContactNo = reg_UserMV.ContactNo;
                    user.GenderID = reg_UserMV.GenderID;
                    user.EmailAddress = reg_UserMV.EmailAddress;
                    user.RegisterationDate = reg_UserMV.RegisterationDate;
                    user.UserStatusID = reg_UserMV.UserStatusID;
                    Db.UserTables.Add(user);
                    Db.SaveChanges();
                    return RedirectToAction("Login","User");
                }
            }
            ViewBag.GenderID = new SelectList(Db.GenderTables.ToList(), "GenderID", "GenderTitle", reg_UserMV.GenderID);
            return View(reg_UserMV);
        }

 Register View Code : 

@model PizzaRestaurantDrink.Models.Reg_UserMV
@{
    ViewBag.Title = "Register";
}

<!-- Breadcrumb Start -->
<div class="bread-crumb">
    <div class="container">
        <div class="matter">
            <h2>Register</h2>
            <ul class="list-inline">
                <li class="list-inline-item"><a href="@Url.Content("~/Home/Index")">HOME</a></li>
                <li class="list-inline-item"><a href="@Url.Content("~/User/Register")">Register</a></li>
            </ul>
        </div>
    </div>
</div>
<!-- Breadcrumb End -->
<!-- User Register Start -->
<div class="login">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-sm-12 commontop text-center">
                <h4>Create an Account</h4>
                <div class="divider style-1 center">
                    <span class="hr-simple left"></span>
                    <i class="icofont icofont-ui-press hr-icon"></i>
                    <span class="hr-simple right"></span>
                </div>
            </div>
            <div class="col-lg-10 col-md-12">
                <div class="row">
                    <div class="col-sm-12 col-md-6">
                        <div class="loginnow" style="height: 920px;">
                            <h5>Register</h5>
                            <p>Do You have an account? So <a href="@Url.Content("~/User/Login")">login</a> And starts less than a minute</p>
                            @using (Html.BeginForm("Register", "User", FormMethod.Post, new { @enctype = "multipart/form-data" }))
                            {
                                @Html.AntiForgeryToken()
                                @Html.ValidationSummary(true, "", new { @class = "text-danger" })

                                <div class="form-group">
                                    <i class="icofont icofont-ui-user"></i>
                                    @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter First Name" } })
                                    @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                                </div>
                                <div class="form-group">
                                    <i class="icofont icofont-ui-user"></i>
                                    @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Last Name" } })
                                    @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                                </div>
                                <div class="form-group">
                                    <i class="icofont icofont-ui-user"></i>
                                    @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter User Name" } })
                                    @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
                                </div>
                                <div class="form-group">
                                    <i class="icofont icofont-ui-message"></i>
                                    @Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Email Address" } })
                                    @Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
                                </div>
                                <div class="form-group">
                                    <i class="icofont icofont-lock"></i>
                                    @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Password" } })
                                    @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                                </div>
                                <div class="form-group">
                                    <i class="icofont icofont-lock"></i>
                                    @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control", @placeholder = "Confirm Password" } })
                                    @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
                                </div>
                                <div class="form-group">
                                    <i class="icofont icofont-phone"></i>
                                    @Html.EditorFor(model => model.ContactNo, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Contact No" } })
                                    @Html.ValidationMessageFor(model => model.ContactNo, "", new { @class = "text-danger" })
                                </div>
                                <div class="form-group">
                                    <i class="icofont icofont-people"></i>
                                    @Html.DropDownList("GenderID", null, "--Choose Gender--", htmlAttributes: new { @class = "form-control" })
                                    @Html.ValidationMessageFor(model => model.GenderID, "", new { @class = "text-danger" })
                                </div>
                                <div class="form-group">
                                    <div class="links">
                                        <label><input type="checkbox" class="checkbox-inline" /> By signing up I agree with <a href="#">terms & conditions.</a> </label>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <input type="submit" value="SIGN UP" class="btn btn-theme btn-md btn-wide" />
                                </div>
                            }
                        </div>
                    </div>

                    <div class="col-sm-12 col-md-6">
                        <div class="loginto loginnow">
                            <h5>Login via social accounts</h5>
                            <ul class="list-unstyled text-center">
                                <li><a href="#" target="_blank"><i class="icofont icofont-social-facebook"></i> Login with Facebook</a></li>
                                <li><a href="#" target="_blank"><i class="icofont icofont-social-google-plus"></i> Login with Google+</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- User Register End -->