Online Restaurant Website Part 10 User Address

     Online Restaurant Website Part 10 Delivery Address 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 implement how to user address table? in asp.net mvc form online restaurant for more details click here: watch video

In this video we are going to add some address type to addresstypetable and also set primary key as identity column of both addresstypetable and useraddresstable. Then we are going to asp.net mvc project to update entity data model, so how to update entity data model on simple why? follow these steps:
first we find to delete addresstypetable and useraddresstable then save the entity data model, 
Second Step: then right click inside in entity data model, goto update model from database option and again add the delete tables and save the changes, so it's done.  for more detail watch video and follow below step's.. 

After Implemented this post, we will get this result View(List_UserAddress) : 


Let's Go to Implement : 
First we are going to create some models:


 > UserAddressMV Model: (Data Format in List) 

 public class UserAddressMV
    {
        public int UserAddressID { get; set; }
        public string AddressType { get; set; }
        public string FullAddress { get; set; }
        public string VisibleStatus { get; set; }
        public string UserName { get; set; }
    }

> CRU_UserAddressMV Model:(Insert, Edit and Retrieve data from database)

 public class CRU_UserAddressMV
    {
        PizzaRestaurentandDrinksDbEntities db = new PizzaRestaurentandDrinksDbEntities();
        public CRU_UserAddressMV()
        {
            GetUserAddress();
        }
        public CRU_UserAddressMV(int? id)
        {
            GetUserAddress();

            var editaddress = db.UserAddressTables.Find(id);
            if (editaddress != null)
            {
                UserAddressID = editaddress.UserAddressID;
                AddressTypeID = editaddress.AddressTypeID;
                FullAddress = editaddress.FullAddress;
                VisibleStatusID = editaddress.VisibleStatusID;
            }
            else
            {
                UserAddressID = 0;
                AddressTypeID = 0;
                FullAddress = string.Empty;
                VisibleStatusID = 0;
            }
        }

        public int UserAddressID { get; set; }
        public int AddressTypeID { get; set; }
        public string FullAddress { get; set; }
        public int VisibleStatusID { get; set; }
        public virtual List<UserAddressMV> List { get; set; }

        private void GetUserAddress()
        {
            List = new List<UserAddressMV>();
            foreach (var address in db.UserAddressTables.ToList())
            {
                List.Add(new UserAddressMV()
                {
                    UserAddressID = address.UserAddressID,
                    AddressType = address.AddressTypeTable.AddressType,
                    FullAddress = address.FullAddress,
                    VisibleStatus = address.VisibleStatusTable.VisibleStatus,
                    UserName = address.UserTable.UserName
                });
            }
        }
    }

> List_UserAddress Action :(Insert, Edit and Retrieve data from database)

 // User Address> ADD , EDIT
        public ActionResult List_UserAddress(int? id)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserTypeID"])))
            {
                return RedirectToAction("Index", "Home");
            }

            int userid = 0;
            if (!string.IsNullOrEmpty(Convert.ToString(Session["UserID"])))
            {
                int.TryParse(Convert.ToString(Session["UserID"]), out userid);
            }
            var address = new CRU_UserAddressMV(id);
            ViewBag.AddressTypeID = new SelectList(Db.AddressTypeTables.ToList(), "AddressTypeID", "AddressType", address.AddressTypeID);
            ViewBag.VisibleStatusID = new SelectList(Db.VisibleStatusTables.ToList(), "VisibleStatusID", "VisibleStatus", address.VisibleStatusID);
            return View(address);
        }

        [HttpPost]
        public ActionResult List_UserAddress(CRU_UserAddressMV cRU_UserAddressMV)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserTypeID"])))
            {
                return RedirectToAction("Index", "Home");
            }

            int userid = 0;
            if (!string.IsNullOrEmpty(Convert.ToString(Session["UserID"])))
            {
                int.TryParse(Convert.ToString(Session["UserID"]), out userid);
            }
            if (ModelState.IsValid)
            {
                if (cRU_UserAddressMV.UserAddressID == 0)
                {
                    var checkexist = Db.UserAddressTables.Where(s => s.FullAddress.ToUpper() == cRU_UserAddressMV.FullAddress.ToUpper() && s.AddressTypeID == cRU_UserAddressMV.AddressTypeID).FirstOrDefault();
                    if (checkexist == null)
                    {
                        var newaddress = new UserAddressTable();
                        newaddress.UserID = userid;
                        newaddress.AddressTypeID = cRU_UserAddressMV.AddressTypeID;
                        newaddress.FullAddress = cRU_UserAddressMV.FullAddress;
                        newaddress.VisibleStatusID = cRU_UserAddressMV.VisibleStatusID;
                        newaddress.CreatedBy_UserID = userid;
                        Db.UserAddressTables.Add(newaddress);
                        Db.SaveChanges();
                        return RedirectToAction("List_UserAddress", new { id = 0 });

                    }
                    else
                    {
                        ModelState.AddModelError("FullAddress", "Already Exist!");
                    }
                }
                else
                {
                    var checkexist = Db.UserAddressTables.Where(s => s.FullAddress.ToUpper() == cRU_UserAddressMV.FullAddress.ToUpper() && s.AddressTypeID == cRU_UserAddressMV.AddressTypeID && s.UserAddressID != cRU_UserAddressMV.UserAddressID).FirstOrDefault();
                    if (checkexist == null)
                    {
                        var edituseraddress = Db.UserAddressTables.Find(cRU_UserAddressMV.UserAddressID);
                        edituseraddress.UserID = userid;
                        edituseraddress.AddressTypeID = cRU_UserAddressMV.AddressTypeID;
                        edituseraddress.FullAddress = cRU_UserAddressMV.FullAddress;
                        edituseraddress.VisibleStatusID = cRU_UserAddressMV.VisibleStatusID;
                        edituseraddress.CreatedBy_UserID = userid;
                        Db.Entry(edituseraddress).State = System.Data.Entity.EntityState.Modified;
                        Db.SaveChanges();
                        return RedirectToAction("List_UserAddress", new { id = 0 });

                    }
                    else
                    {
                        ModelState.AddModelError("FullAddress", "Already Exist!");
                    }
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Fill All Field Properly.");
            }
            ViewBag.AddressTypeID = new SelectList(Db.AddressTypeTables.ToList(), "AddressTypeID", "AddressType", cRU_UserAddressMV.AddressTypeID);
            ViewBag.VisibleStatusID = new SelectList(Db.VisibleStatusTables.ToList(), "VisibleStatusID", "VisibleStatus", cRU_UserAddressMV.VisibleStatusID);
            return View(cRU_UserAddressMV);
        }


 > List_UserAddress View : 

 @model PizzaRestaurantDrink.Models.CRU_UserAddressMV
@{
    ViewBag.Title = "User Address";
}
<!-- Breadcrumb Start -->
<div class="bread-crumb">
    <div class="container">
        <div class="matter">
            <h2>User Address</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("~/Setting/List_UserAddress")">User Address</a></li>
            </ul>
        </div>
    </div>
</div>
<!-- Breadcrumb End -->
<div class="contactus">
    <div class="container">
        <div class="row">
            <!-- Title Content Start -->
            <div class="col-sm-12 commontop text-center">
                <h4>User Addresses</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>
            <!-- Title Content End -->

            <div class="col-md-5 col-12">
                <!--  user type form Start  -->
                @using (Html.BeginForm("List_UserAddress", "Setting", FormMethod.Post, new { @enctype = "multipart/form-data", @class = "form-horizontal" }))
                {
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    @Html.HiddenFor(model => model.UserAddressID)
                    <div class="form-group row">
                        <div class="col-md-12 col-sm-12 col-12">
                            @Html.DropDownList("AddressTypeID", null, "--Choose Type--", htmlAttributes: new { @class = "form-control" })
                            @Html.ValidationMessageFor(model => model.AddressTypeID, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group row">
                        <div class="col-md-12 col-sm-12 col-12">
                            <i class="icofont icofont-ui-user"></i>
                            @Html.EditorFor(model => model.FullAddress, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Full Address" } })
                            @Html.ValidationMessageFor(model => model.FullAddress, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group row">
                        <div class="col-md-12 col-sm-12 col-12">
                            @Html.DropDownList("VisibleStatusID", null, "--Choose Status--", htmlAttributes: new { @class = "form-control" })
                            @Html.ValidationMessageFor(model => model.VisibleStatusID, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="buttons">
                        @if (Model.UserAddressID == 0)
                        {
                            <input class="btn btn-theme btn-md btn-wide" style="float:right;" type="submit" value="Create" />
                        }
                        else
                        {
                            <input class="btn btn-theme btn-md btn-wide" style="float:right;" type="submit" value="Update" />
                        }
                    </div>
                }
                <!--  user type form End  -->
            </div>
            <div class="col-md-7 col-12">
                <!--  List Start  -->
                <div class="table-responsive-md">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <td class="text-center">
                                    Address Type
                                </td>
                                <td class="text-center">
                                    Full Address
                                </td>
                                <td class="text-center">
                                    Status
                                </td>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.List)
                            {
                                <tr>
                                    <td>
                                        @Html.DisplayFor(modelItem => item.AddressType)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelItem => item.FullAddress)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelItem => item.VisibleStatus)
                                    </td>
                                    <td>
                                        @Html.ActionLink("Edit", "List_UserAddress", new { id = item.UserAddressID }, new { @class = "btn btn-theme btn-md btn-wide" })
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
                <!--  List End  -->
            </div>
        </div>
    </div>
</div>