Asp.net MVC

Inventory Management ASP.NET MVC with Ajax

This tutorial will teach you how to make a Inventory Management System in ASP.NET MVC with Ajax step by step. This system will helpful you to learn Inventory Management System.this system helpful for you learn sales handling process of asp.net mvc with ajax.

Functional requirements

The System shall be able to find the barcode id of the products.in order to find the product details enter the barcode id in to the barcode id textbox then it will automatically find product details without loading page and produce the output in to the related textboxes.

This System shall be able to store the Sales details in to the SQLserver databases.after sales has been done.

The System shall be able to release the print receipt for print the bills.

Let learn how to do the system step by step

First go and open  the visual studio and create the new project.

After that Select the language as Visual C# with ASP.NET Web Application

Click Ok

Select MVC Click Ok

First we have to study MVC Folder of project MVC stands for model-view-controller.

i have attached the folder structure above(Models,Views,Controllers)

this course i am going to cover about database first approach.for deal with models folder first.

Models

Create the database on the SQLserver.i have create database which name is possystem.

Product Table

Sales

Sales_product

these are the table you have create on the SqlServer.   id set as primarykey along with auto increment.i have attach the screen shot image below. How to do it.

select the id column then go the id  column  properties below do it as Is Identity Yes.

After created the all the tables  then go the the visual studio back.

select model folder and right click do the following steps i attached screenshot image below.

Select  Data with ADO.NET Entity Data Model

Click Add after that pop up Generate from database click Next.

Establish the Database Connection

Click new Connection

Configure the Connection Properties

select database name and give username and password while you installing the SQLserver. and test the connection
if the connection has success click OK Button.

Select the table

Click finish

Model.edmx diagram is created successfully.

After that select you project install the following libraries.

Newtonsoft.Json
jquery
bootstarp

After that select the home controller paste this code.i attached video tutorial below watch and do it.

go to the controller folder and double click the  HomeController  paste the following code

HomeController

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication29.Models;

namespace WebApplication29.Controllers
{
    public class HomeController : Controller
    {
        //you don't need this here except you want to use Dependency injection

        possytemEntities10 dc = new possytemEntities10();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Print()
        {
            ajaxModel model = (ajaxModel)TempData["Data"];
            //reassign temp data for reload purposes
            TempData["Data"] = model;
            return View(model);
        }

        [HttpPost]
        public JsonResult Getid(String Id)
        {
            //Get the products from products sample collection for demo purpose.
            //Get the products from the database in the real application
            var std = dc.products.Where(a => a.id == Id).FirstOrDefault();
            //return new JsonResult { Data = new { std = std } };
            return Json(std, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public JsonResult Save(string data)
        {
            ajaxModel model = JsonConvert.DeserializeObject<ajaxModel>(data);
            bool status = false;
            try
            {
                using (possytemEntities10 dc = new possytemEntities10())
                {
                    //insert into Sales table
                    var sale = new sale
                    
                    {
                      
                      date = DateTime.Now.ToString("d"), //ToString was used because the date in the model was string
                      subtotal = model.total
                  };
                   dc.sales.Add(sale);
                   dc.SaveChanges();

                    model.data.ForEach(m =>
                    {
                        //get the product and deduct the subtotal
                        var db_product = dc.products.First(e => e.id == m.barcode);                     
                        db_product.qty = db_product.qty - m.qty;

                        dc.sales_product.Add(new sales_product
                        {
                            sales_id = sale.id,
                            product_id = int.Parse(m.barcode),
                            price = m.pro_price,
                            qty = m.qty,
                            total = m.total_cost
                        });
                    });
                    dc.SaveChanges();
                    status = true;
                }
                return new JsonResult { Data = new { status, message = "Entry saved successfully" } };
            }
            catch (Exception e)
            {
                return new JsonResult { Data = new { status, message = "There was an error saving the Entry" } };
            }

        }

        [HttpPost]
        public ActionResult SaveNew(string data)
        {
            ajaxModel model = JsonConvert.DeserializeObject<ajaxModel>(data);
            bool status = false;
            try
            {
                using (possytemEntities10 dc = new possytemEntities10())
                {
                    //insert into Sales table
                    var sale = new sale
                    {
                        date = DateTime.Now.ToString("d"), //ToString was used because the date in the model was string
                        subtotal = model.total
                    };
                    dc.sales.Add(sale);
                    dc.SaveChanges();
                    
                   
                    model.data.ForEach(m =>
                    {
                        //get the product and deduct the subtotal
                        var db_product = dc.products.First(e => e.id == m.barcode);
                        db_product.qty = db_product.qty - m.qty;

                        dc.sales_product.Add(new sales_product
                        {
                            sales_id = sale.id,
                            product_id = int.Parse(m.barcode),
                            price = m.pro_price,
                            qty = m.qty,
                            total = m.total_cost
                        });
                    });
                    dc.SaveChanges();
                    model.date = sale.date;
                    model.orderId = sale.id;
                    status = true;
                }
                TempData["Data"] = model;
                return new JsonResult { Data = new { status, message = "Entry saved successfully"} };
                //return RedirectToAction("Print", model);
            }
            catch (Exception e)
            {
                return new JsonResult { Data = new { status, message = "There was an error saving the Entry" } };
            }

        }

        public string sales_id { get; set; }
    }
}

After that  go the views folder there is the folder which name Shared inside the Shared folder there is the file which name is layout.cshtml. this is the layout file.

Layout File

Layout file is a common file for each page. Example you have the header and footer in your website.  header and footer are common for each pages.it called as layout.content would be changed each pages like about us pages displays regarding about us content,Contact us pages displays regarding Contact us content.like that.layout.cshtml paste the following codes.

layout.cshtml 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @RenderSection("styles", false)
</head>
<body>
    
    <div class="container-fluid bg-2 text-center">
        @RenderBody()
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

After done that go the view folder inside the views folder there is folder Home inside the home folder folder paste the  following code.

index.cshtml

@{
    ViewBag.Title = "Index";
}

<div class="row">
    <div class="col-sm-8">
        @using (Html.BeginForm("Index", "home", FormMethod.Post, new { id = "popupForm" }))
        {

            <table class="table table-bordered">
                <caption> Add Products  </caption>
                <tr>
                    <th>Product Code</th>
                    <th>Product Name</th>
                    <th>Price</th>
                    <th>Qty</th>

                    <th>Amount</th>
                    <th>Option</th>
                </tr>
                <tr align="center">
                    <td>
                        <input type="text" class="form-control" placeholder="barcode" id="barcode" name="barcode" size="25px" required>
                    </td>
                    <td>
                        <input type="text" class="form-control" placeholder="barcode" id="pname" name="pname" size="50px" disabled>
                        @*<label id="pro_name" name="pname" id="pname"></label>*@
                    </td>
                    <td>
                        <input type="text" class="form-control pro_price" id="pro_price" size="25px" name="pro_price"
                               placeholder="price" disabled>
                    </td>
                    <td>
                        <input type="number" class="form-control pro_price" id="qty" name="qty"
                               placeholder="qty" min="1" value="1" size="10px" required>
                    </td>

                    <td>
                        <input type="text" class="form-control" placeholder="total_cost" size="35px" id="total_cost" name="total_cost" disabled>
                    </td>
                    <td>
                        <button class="btn btn-success" type="button" >

after that create file print.cshtml paste the following code.

@using WebApplication29.Models
@model ajaxModel
@{
    var count = 0;
}
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <style>        
    </style>
</head>
<body style='background: #f9f9f9'>

    <script>
        n = new Date();
        y = n.getFullYear();
        m = n.getMonth() + 1;
        d = n.getDate();
        document.getElementById("date").innerHTML = m + "/" + d + "/" + y;

    </script>

    <div class="container">
        <div class="row">
            <div class="col-xs-12">
                <div class='print' style="border: 1px solid #a1a1a1; font-family: monospaced; width: 88mm; background: white; padding: 10px; margin: 0 auto; text-align: center; ">


                    <div class="invoice-title" align="center">

                        <b><tt> Print INVOICE</b><h2>Inventory</h2></tt>
                    </div>

                    <div class="invoice-title" align="left" style=" font-family: monospaced;">
                        <tt>    Order #  &nbsp; &nbsp;<b> @Model.orderId</b> </tt>
</div>
                    <div class="invoice-title" align="left">
                        <tt>    Date #  &nbsp; <b> @Model.date </b> </tt>
</div>



                    <div>
                        <div>

                            <table class="table table-condensed">
                                <thead>
                                    <tr>
                                                  <td class="text-center"><strong><tt> No </tt></strong></td> 
                                          <td class="text-center"><strong><tt>Productname </tt></strong></td> 
                                         <td class="text-center"><strong><tt>Qty</tt></strong></td> 
                                        <td class="text-center"><strong><tt>Price</tt></strong></td>
                                        @*<td class="text-right"><strong>Total</strong></td>*@
                                    </tr>
                                </thead>
                              
                                @foreach (tableViewModel tableModel in Model.data)
                                {
                                    
                                    <tr>
                                        <td class="text-center">
                                            <tt>           @(count = count + 1)   <tt>
                                        </td>
                                        <td class="text-center">
                                    <tt>        @tableModel.pname   </tt>
                                        </td>
                                        <td class="text-center"> 
                                    <tt>        @tableModel.qty   </tt>
                                        </td>
                                        <td class="text-center">
                                 <tt>           @tableModel.pro_price   </tt>
                                        </td>
                                        @*<td class="text-right"></td>*@
                                    </tr>
                                }

                             
                            </table>

                        </div>
                    </div>


                    <div align="right">
                        <tt>      Sub Total &nbsp;&nbsp;<b> @Model.total</b>  </tt>
                    </div>
                   

                    <div align="center">
                             <i><tt> 60 b sea street India </tt></i>  
                    </div>



                </div>


            </div>
            </div>
        </div>

</body>
@Scripts.Render("~/bundles/jquery")

@section scripts{

    <script src="~/Scripts/jquery-3.4.1.js"></script>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>


    <script src="~/Scripts/jquery.validate.js"></script>




    <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.js"></script>


    <script>

        myFunction();
        //
        window.onafterprint = function (e) {
            closePrintView();
        };

        function myFunction() {
            window.print();

        }
        function closePrintView() {
            window.location.href = '@Url.Action("Index", "Home")';

        }

    </script>

    }

</html>


@section styles{
    <link href="~/Content/bootstrap.css" rel="stylesheet" />

    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.css">
    <link rel="stylesheet" href="//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">

    <style type="text/css">

            @@media print
            {
                .button
                {
                    display: none;
                }
            }
            @@media print
            {
                @@page {
                    margin-top: 0;
                    margin-bottom: 0;
                }
                body  {
                    padding-top: 72px;
                    padding-bottom: 72px ;
                }
            }



</style>



}

i have attached the video link below. which will do this tutorials step by step

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

admin

Recent Posts

Laravel 11 CRUD Mastering RESTful API MVC with Repository Pattern

In this tutorial will teach Laravel 11 Api MVC with Repository Pattern Crud Application step…

1 day ago

Laravel 11 CRUD Application

In this tutorial will teach Laravel 11 CRUD Application step by step. Laravel  11 CRUD…

4 weeks ago

How to make Times Table in React

in this tutorials we will be talk about how to make a times table in…

1 month ago

Laravel Tutorial: How to Add Numbers Easily Laravel 10

In this tutorials will teach How to add two numbers in Laravel 10. (more…)

1 month ago

Build Full-Stack Node.js MongoDB CRUD App with JWT Authentication

In this tutorial, we will teach the process of building a full-stack application using Node.js,…

2 months ago

Hospital Management System using OOP Java and MySQL

In this tutorial, we will explin you through the process of building a Hospital Management…

3 months ago