eCommerce Website Part 24 Units Table

 eCommerce Website  Part 24 Units Table

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 implement UnitTable form for more details click here: watch vedio

So Now we are going to Create Units Action to Add , Edit and retrieve product units
So let get start..

> UnitMV Code

 public class UnitMV

    {

        [Display(Name="#Unique No")]

        public int UnitID { get; set; }

        [Display(Name = "#Unit Title")]

        public string UnitTitle { get; set; }

    }

> Reg_UnitMV Code

public class Reg_UnitMV

    {

        public Reg_UnitMV()

        {

            GetUnits();

        }

        public Reg_UnitMV(int? id)

        {

            GetUnits();

            var edit = new DatabaseLayer.Pro_EcommerceDbEntities().UnitTables.Find(id);

            if (edit != null)

            {

                UnitID = edit.UnitID;

                UnitTitle = edit.UnitTitle;

            }

            else

            {

                UnitID = 0;

                UnitTitle = string.Empty;

            }

        }

        [Display(Name = "#Unique No")]

        public int UnitID { get; set; }


        [Required(ErrorMessage = "Required*")]

        [Display(Name = "Unit Title")]

        public string UnitTitle { get; set; }

        public List<UnitMV> List_Units { get; set; }


        public void GetUnits()

        {

            List_Units = new List<UnitMV>();

            foreach (var unit in new DatabaseLayer.Pro_EcommerceDbEntities().UnitTables.ToList())

            {

                List_Units.Add(new UnitMV()

                {

                     UnitID = unit.UnitID,

                     UnitTitle = unit.UnitTitle

                }); 

            }

        }

    } 

 > Product Unit Action Code

      Action  Code :  

        public ActionResult Units(int? id)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserID"])))
            {
                return RedirectToAction("Login", "User");
            }
            int userid = 0;
            int.TryParse(Convert.ToString(Session["UserID"]), out userid);
            var unit = new Reg_UnitMV(id);
            return View(unit);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Units(Reg_UnitMV reg_UnitMV)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserID"])))
            {
                return RedirectToAction("Login", "User");
            }
            int userid = 0;
            int.TryParse(Convert.ToString(Session["UserID"]), out userid);

            if (ModelState.IsValid)
            {
                if (reg_UnitMV.UnitID == 0)
                {
                    var checkexist = DB.UnitTables.Where(u => u.UnitTitle == reg_UnitMV.UnitTitle).FirstOrDefault();
                    if (checkexist == null)
                    {
                        var create = new UnitTable();
                        create.UnitTitle = reg_UnitMV.UnitTitle;
                        DB.UnitTables.Add(create);
                        DB.SaveChanges();
                        return RedirectToAction("Units", new { id = 0 });
                    }
                    else
                    {
                        ModelState.AddModelError("UnitTitle", "Already Added!");
                    }
                }
                else
                {
                    var checkexist = DB.UnitTables.Where(u => u.UnitTitle == reg_UnitMV.UnitTitle && u.UnitID != reg_UnitMV.UnitID).FirstOrDefault();
                    if (checkexist == null)
                    {
                        var edit = DB.UnitTables.Find(reg_UnitMV.UnitID);
                        edit.UnitTitle = reg_UnitMV.UnitTitle;
                        DB.Entry(edit).State = System.Data.Entity.EntityState.Modified;
                        DB.SaveChanges();
                        return RedirectToAction("Units", new { id = 0 });
                    }
                    else
                    {
                        ModelState.AddModelError("UnitTitle", "Already Added!");
                    }
                }
            }
            return View(reg_UnitMV);
        }


> Units Views Code       

@model eCommerceUI.Models.Reg_UnitMV
@{
    ViewBag.Title = "Unit's";
}
<div class="container">
    <div class="page-width">
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            <hr />
            <h3 class="docs-title text-center">Product Unit's</h3>
            <div class="form-horizontal">
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.HiddenFor(model => model.UnitID)
                <div class="row" style="margin-left:1px;">
                    <div class="form-group col-md-3">
                        @Html.LabelFor(model => model.UnitTitle, "Enter Unit Title", htmlAttributes: new { @class = "control-label" })
                        @Html.EditorFor(model => model.UnitTitle, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.UnitTitle, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        @if (Model.UnitID == 0)
                        {
                            <input type="submit" value="Create" class="btn btn--secondary" />
                        }
                        else
                        {
                            <input type="submit" value="Update" class="btn btn--secondary" />
                        }
                    </div>
                </div>
            </div>

        }
        <hr />
        <h3 class="docs-title">Product Unit's</h3>
        <form autocomplete="off">
            <p>
                <div style="float:right;">
                    <input type="text"
                           style="background-image: url('@Url.Content("~/Content/Template/images/searchicon.png")');
                           background-position: 10px 5px;
                           background-repeat: no-repeat;
                           font-size: 16px;
                           padding: 12px 20px 12px 40px;
                           border: 1px solid #ddd;
                           margin-bottom: 12px; "
                           class="search form-control"
                           id="myInput"
                           placeholder="What you looking for?">
                </div>
            </p>
        </form>

        <div class="table">
            <table class="responsive-table">
                <thead>
                    <tr>
                        <td>#Unique No</td>
                        <td>Product Unit</td>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody id="myTable">
                    @foreach (var item in Model.List_Units)
                    {
                        <tr class="responsive-table-row">
                            <td>#@Html.DisplayFor(modelItem => item.UnitID)</td>
                            <td>
                                @Html.DisplayFor(modelItem => item.UnitTitle)
                        </td>
                        <td>
                            @Html.ActionLink("Edit", "Units", new { id = item.UnitID }, new { @class = "btn" })
                        </td>
                    </tr>
                }
                <tbody>
            </table>
        </div>
    </div>
</div>

Comments

Post a Comment