eCommerce Website Part 7 Address Types

  eCommerce Website  Part 7 Address Types

Hi, Dear's here we learn how to implement eCommerce 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 design Address Types views form for more details click here:

First Download Materials Download Materials.

Create BasicConfigurationController and create one by one below action's:

Address Types: 

 > Address Types List

      Action  Code :  

            public ActionResult AddressTypes_List()
        {
            var list = new List<AddressTypeMV>();
            foreach (var addresstype in DB.AddressTypeTables.ToList())
            {
                list.Add(new AddressTypeMV()
                {
                    AddressTypeID = addresstype.AddressTypeID,
                    AddressType = addresstype.AddressType
                });
            }
            return View(list);
        }

        Views Code :  

@model IEnumerable<eCommerceUI.Models.AddressTypeMV>
@{
    ViewBag.Title = "Address Types";
}
<div class="container">
    <div class="page-width">

        <h3 class="docs-title">Address Types List</h3>
        <p>
            @Html.ActionLink("New Address Type", "NewAddressType", null, new { @class = "btn btn--secondary" })
        </p>

        <div class="table-wrap">
            <table class="responsive-table">
                <thead>
                    <tr>
                        <th>@Html.DisplayNameFor(model => model.AddressTypeID)</th>
                        <th>@Html.DisplayNameFor(model => model.AddressType)</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr class="responsive-table-row">
                            <td>#@Html.DisplayFor(modelItem => item.AddressTypeID)</td>
                            <td>@Html.DisplayFor(modelItem => item.AddressType)</td>
                            <td>
                                @Html.ActionLink("Edit", "EditAddressType", new { id = item.AddressTypeID }, new { @class = "btn" })
                            </td>
                        </tr>
                    }
                <tbody>
            </table>
        </div>
    </div>
</div>

> New Address Type :

        Action Code :  

        public ActionResult NewAddressType()
        {
            return View(new AddressTypeMV());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult NewAddressType(AddressTypeMV addressTypeMV)
        {
            if (ModelState.IsValid)
            {
                var check_exist = DB.AddressTypeTables.Where(a => a.AddressType == addressTypeMV.AddressType.Trim()).FirstOrDefault();
                if (check_exist == null)
                {
                    var addresstype = new AddressTypeTable();
                    addresstype.AddressType = addressTypeMV.AddressType;
                    DB.AddressTypeTables.Add(addresstype);
                    DB.SaveChanges();
                    return RedirectToAction("AddressTypes_List");
                }
                else
                {
                    ModelState.AddModelError("AddressType", "Address Type Already Registered.");
                }
            }
            ModelState.AddModelError("AddressType", "Address Type is Required");
            return View(addressTypeMV);
        }

 View  Code :  


@model eCommerceUI.Models.AddressTypeMV
@{
    ViewBag.Title = "New Address Type";
}
<div class="container">
    <div class="page-width">
        @using (Html.BeginForm("NewAddressType", "BasicConfiguration"))
        {
            @Html.AntiForgeryToken()

            <h3 class="docs-title">New Address Type</h3>
            <hr />
            <div class="form-horizontal">

                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(model => model.AddressType, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.AddressType, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Address Type" } })
                        @Html.ValidationMessageFor(model => model.AddressType, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Create" class="btn btn--secondary" />
                        @Html.ActionLink("Back to List", "AddressTypes_List", null, new { @class = "btn" })
                    </div>
                </div>
            </div>
        }
    </div>
</div>

> Edit Address Type:

       Action Code :  

            public ActionResult EditAddressType(int? id)
        {
            var addresstype = DB.AddressTypeTables.Find(id);
            var editaddresstype = new AddressTypeMV();
           editaddresstype.AddressTypeID = addresstype.AddressTypeID;
            editaddresstype.AddressType = addresstype.AddressType;
            return View(editaddresstype);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditAddressType(AddressTypeMV addressTypeMV)
        {
            if (ModelState.IsValid)
            {
                var check_exist = DB.AddressTypeTables.Where(a => a.AddressType == addressTypeMV.AddressType.Trim() && a.AddressTypeID != addressTypeMV.AddressTypeID).FirstOrDefault();
                if (check_exist == null)
                {
                    var addresstype = DB.AddressTypeTables.Find(addressTypeMV.AddressTypeID);
                    addresstype.AddressType = addressTypeMV.AddressType;
                    DB.Entry(addresstype).State = System.Data.Entity.EntityState.Modified;
                    DB.SaveChanges();
                    return RedirectToAction("AddressTypes_List");
                }
                else
                {
                    ModelState.AddModelError("AddressType", "Address Type Already Registered.");
                }
            }
            ModelState.AddModelError("AddressType", "Address Type is Required");
            return View(addressTypeMV);
        }

  View  Code :

           @model eCommerceUI.Models.AddressTypeMV
@{
    ViewBag.Title = "Edit";
}
<div class="container">
    <div class="page-width">
        @using (Html.BeginForm("EditAddressType", "BasicConfiguration"))
        {
            @Html.AntiForgeryToken()
            <h3 class="docs-title">Edit Address Type</h3>
            <hr />
            <div class="form-horizontal">
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.HiddenFor(model => model.AddressTypeID)

                <div class="form-group">
                    @Html.LabelFor(model => model.AddressType, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.AddressType, new { htmlAttributes = new { @class = "form-control", placeholder="Enter Address Type" } })
                        @Html.ValidationMessageFor(model => model.AddressType, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Save" class="btn btn--secondary" />
                        @Html.ActionLink("Back to List", "AddressTypes_List", null, new { @class = "btn" })
                    </div>
                </div>
            </div>
        }
    </div>
</div>

Comments