Thumb

Part-17: Inventory and POS Batch CRUD using asp.net MVC JQUERY AJAX | CSharp

Part-1: Point of Sale(POS) Inventory Super Shop Management System using ASP.NET MVC Part-2: Point of Sale(POS) Setup template to your project | Super Shop Management System|ASP.NET MVC Part-3: Point of Sale(POS) Database & table analysis | Super Shop Management System|ASP.NET MVC Part-4: Point of Sale(POS) Database Create in SQL Server | Super Shop Management System|ASP.NET MVC Part-5: Point of Sale(POS) Login using AJAX ASP.NET MVC | JQUERY Part-6: Point of Sale(POS) Login and Authorization with Session variables in ASP.NET | JQUERY | AJAX Part-7: Point of Sale(POS) Convert Password to MD5 Hash string in ASP.NET | Encrypt password in ASP.NET MVC C# Part-8: Point of Sale(POS) Role based authentication and authorization in ASP.NET MVC|Redirect to not found page if not authorized|C# Part-9: Point of Sale(POS) Create user & Account management UI UX in ASP.NET MVC Part-10: Point of Sale(POS) User Creation & user registration using ASP.NET MVC | Jquery | Ajax Part-11: Point of Sale(POS) Get user list using ASP.NET MVC | Jquery | Ajax Part-12: Point of Sale(POS) Update user using ASP.NET MVC | Jquery | Ajax Part-13: Inventory and POS Login logout session using ASP.NET | Supershop management system Part-14: Inventory Category CRUD Create,Retrieve,Update,View | POS Category CRUD using ASP.NET JQuery AJAX Part-15: Inventory and POS batch tracking and control table create in SQL Server 2012 Part-16: Inventory Product CRUD List | Point of sale Products Crud using asp.net MVC Part-17: Inventory and POS Batch CRUD using asp.net MVC JQUERY AJAX | CSharp Part-18: Inventory management and stock control using asp.net mvc | Jquery AngularJs

1/12/2021 1:02:08 AM

Post your any code related problem to www.abctutorial.com

Follow the previous video and articles to complete the POS tutorials

Step-1

Here we are going to create CRUD operation of Batch which will use in entire POS & Inventory Project. Actually this is the CRUD operation which I showed you in several. If you follow this article then you would be able to do the CRUD operation as well.

  • Please add a method under HomeController
        AuthorizationFilter]
        public ActionResult Batch()
        {
            return View();
        }
  • Add below code into Batch.cshtml.
@{
    ViewBag.Title = "Batch";
}
<div class="col-xl-12 col-md-12 mb-12">
    <div class="card border-left-primary shadow h-100 py-2">
        <div class="card-body">
            <div class="row no-gutters align-items-center">
                <div class="col mr-2">

                    <form class="user">

                        <div class="form-group col-md-6">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Create Batch</div>
                        </div>
                        <input type="hidden" id="batchId" />
                        <div class="form-group col-md-6">
                            <input type="text" disabled class="form-control form-control-user" id="batchName" aria-describedby="emailHelp" placeholder="Batch" value="Batch">
                        </div>
                        <div class="form-group col-md-6">
                            <a href="#" onclick="SaveBatch()" class="btn btn-primary btn-sm">
                                Save
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<br />
<div class="col-xl-12 col-md-12 mb-12">
    <div class="card border-left-primary shadow h-100 py-2">
        <div class="card-body">
            <div class="row no-gutters align-items-center">
                <div class="col mr-2">
                    <div class="form-group col-md-6">
                        <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Batch List</div>
                    </div>

                    <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>
                                    Action
                                </th>
                            </tr>
                        </thead>
                        <tbody id="trDiv"></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

Step-2

The main part of this Batch CRUD is saving, updating, retrieving data so here I create and update data using one single method SaveCategory

  • Please go to HomeController
  • Add this below method inside the HomeController
        [HttpPost]
        public JsonResult SaveBatch(Batch batch)
        {
            PointOfSale.Helper.AppHelper.ReturnMessage retMessage = new AppHelper.ReturnMessage();
            POS_TutorialEntities db = new POS_TutorialEntities();
            retMessage.IsSuccess = true;

            if (batch.BatchId > 0)
            {
                db.Entry(batch).State = EntityState.Modified;
                retMessage.Messagae = "Update Success!";
            }
            else
            {
                batch.BatchName = batch.BatchName + db.Batches.Count();
                var batchData = db.Batches.Where(x => x.BatchName.Equals(batch.BatchName)).SingleOrDefault();
                if (batchData == null)
                {
                    db.Batches.Add(batch);
                    retMessage.Messagae = "Save Success!";
                }
                else
                {
                    retMessage.IsSuccess = false;
                    retMessage.Messagae = "This batch already exist!Please refresh and again try!";
                }
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                retMessage.IsSuccess = false;
            }

            return Json(retMessage, JsonRequestBehavior.AllowGet);
        }

Step-3

As a part of CRUD, to updating data we must show list of data to select for updating. Please follow below steps to

  • Please go to HomeController.
  • Please write this below code to show list of batch.
        [HttpGet]
        public JsonResult GetAllBatch()
        {
            POS_TutorialEntities db = new POS_TutorialEntities();
            var dataList = db.Batches.ToList();
            var modefiedData = dataList.Select(x => new
            {
                BatchId = x.BatchId,
                BatchName = x.BatchName,
            }).ToList();
            return Json(modefiedData, JsonRequestBehavior.AllowGet);
        }       

Step-4

As we are implementing CRUD operation here we used JQUERY AJAX JSON so we will add few JavaScript code which will save, update, retrieve data.

  • Please go to Batch.cshtml view.
  • Please add this below code in below of all HTML code.
<script>
    $(function () {
        GetAllBatch();
    });

    function SaveBatch() {
        var batch = new Object();
        batch.BatchId = $("#batchId").val();
        batch.BatchName = $("#batchName").val();
        var data = JSON.stringify({
            batch: batch
        });
        return $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'POST',
            url: "/Home/SaveBatch",
            data: data,
            success: function (result) {
                if (result.IsSuccess == true) {
                    GetAllBatch();
                    alert(result.Messagae);
                }
                else {
                    alert(result.Messagae);
                }
            },
            error: function () {
                alert("Error!")
            }
        });
    }

    function GetAllBatch() {
        $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'Get',
            url: "/Home/GetAllBatch",
            success: function (data) {
                $("#trDiv").html('');
                var tdContent = '';
                for(var i = 0; i < data.length; i++)
                {
                    tdContent += '<tr><td>' + data[i].BatchName + '<td><a href="#" onclick="Edit(' + data[i].BatchId + ",'" + data[i].BatchName + "'"+');">Edit</a></td></tr>'; // if Name is property of your Person object
                }
                $("#trDiv").append(tdContent);
            },
            error: function () {
                alert("Error!")
            }
        });
    }
    function Edit(batchId, batchName) {
        $("#batchId").val(batchId);
        $("#batchName").val(batchName);
    }
    function Reset() {
        $("#batchId").val(null);
        $("#batchName").val(null);
    }
</script>
  • Now run the project.
  • Then save, update, retrieve data.

About Teacher