Create Product Grid Application With .Net MVC And EF with CRUD Operations
Create Grid Based Web Application for CRUD(Create,Read,Delete,Update) Operation Using .Net MVC 5 And SQL Server
Step 1 : Create Required Tables in Data Base to Store the Details About the Product
CREATE TABLE [dbo].[ProductDetails](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[ProductName] [varchar](50) NULL,
[Quantity] [varchar](50) NULL,
[Prize] [bigint] NULL,
CONSTRAINT [PK_ProductDetails] PRIMARY KEY CLUSTERED
(
[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
So here it will Create Table ProductDetails in your Database.
Step 2: Create MVC Project in Visual Studio as below Options :
select option Basic in next Step...
Let me know in comments if you find any problem while implementing the same :)
Step 1 : Create Required Tables in Data Base to Store the Details About the Product
CREATE TABLE [dbo].[ProductDetails](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[ProductName] [varchar](50) NULL,
[Quantity] [varchar](50) NULL,
[Prize] [bigint] NULL,
CONSTRAINT [PK_ProductDetails] PRIMARY KEY CLUSTERED
(
[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
So here it will Create Table ProductDetails in your Database.
Step 2: Create MVC Project in Visual Studio as below Options :
select option Basic in next Step...
Step 3 : Add New ADO Entity Model And give Name to Entity Model as .edmx file in your project
Step 4 :After Creating Model.edmx Generate Model From Database
Step 4 : Select your Connection Details And Select Next in Case you don't have any Connections then Create New Connection And Configure New Connection And Select your Database.
Step 5 : Make Sure You Select All the Tables that are you wan to use in the Application And Click Finish :
Now You are Ready for the Database Side as Entity Part is Created And Respective Model is Created in c# code side.
Step 6 : Create New Controller As shopping Controller And Add below Methods to the Shopping Controller.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ShoppingApplication.Controllers
{
public class ShoppingController : Controller
{
//
// GET: /Shopping/
public ActionResult Index()
{
WebShoppingEntities db = new WebShoppingEntities();
return View(db.ProductDetails.ToList());
}
[HttpPost]
public ActionResult Index(string name)
{
WebShoppingEntities db = new WebShoppingEntities();
List<ProductDetail> emp = db.ProductDetails.Where(e => e.ProductName.Contains(name)).ToList();
return View(emp);
}
public ActionResult Add()
{
return PartialView();
}
[HttpPost]
public ActionResult Add(ProductDetail productDetail)
{
WebShoppingEntities db = new WebShoppingEntities();
if (ModelState.IsValid)
{
try
{
db.ProductDetails.Add(productDetail);
db.SaveChanges();
}
catch (Exception ex)
{
TempData["Failure"] = ex;
}
}
else
{
TempData["Failure"] = "Record Not Saved";
}
return RedirectToAction("Index");
}
public ActionResult Edit(int productId)
{
WebShoppingEntities db = new WebShoppingEntities();
return PartialView(db.ProductDetails.Find(productId));
}
[HttpPost]
public ActionResult Edit(ProductDetail productDetail)
{
WebShoppingEntities db = new WebShoppingEntities();
if (ModelState.IsValid)
{
try
{
ProductDetail productdetail = db.ProductDetails.Where(x => x.ProductId == productDetail.ProductId).FirstOrDefault();
productdetail.ProductName = productDetail.ProductName;
productdetail.Quantity = productDetail.Quantity;
productdetail.Prize = productDetail.Prize;
db.SaveChanges();
return RedirectToAction("Index");
}
catch (Exception ex)
{
TempData["Failure"] = ex;
}
}
else
{
TempData["Failure"] = "Record Not Saved";
}
return RedirectToAction("Index");
}
public ActionResult DeleteProduct(int productId)
{
WebShoppingEntities db = new WebShoppingEntities();
db.ProductDetails.Remove(db.ProductDetails.Find(productId));
db.SaveChanges();
return RedirectToAction("Index");
}
}
}
Step 7 : Create View Index For Main Page And Create Partial View For Edit And Add Product Part As below.
1. Index.cshtml
@model IEnumerable<ShoppingApplication.ProductDetail>
@{
ViewBag.Title = "Product List";
Layout = "~/Views/Shared/_Layout.cshtml";
<style type="text/css">
.grid{ width:100%; }
</style>
}
<div id="div-main">
<br />
<br />
<div class="container">
<div class="row">
@* <div class="col-md-6">
<select class="form-control">
<option>---select---</option>
<option>1-5</option>
<option>1-10</option>
<option>1-15</option>
<option>1-20</option>
<option>1-25</option>
<option>1-30</option>
<option>1-35</option>
<option>1-40</option>
<option>1-45</option>
<option>1-50</option>
</select>
</div>*@
<div class="col-md-5">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1" style="cursor: pointer;"><i class="glyphicon glyphicon-search"></i></span>
<input type="text" id="search-box" class="form-control" placeholder="Search by Name" aria-describedby="basic-addon1">
</div>
</div>
<div class="col-md-3 text-info">
<a href="@Url.Action("Add", "Shopping")" id="add">Add New Product</a>
</div>
</div>
<div class="clearfix"> </div>
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr class="success">
<th class="text-center">ProductId</th>
<th class="text-center">ProductName</th>
<th class="text-center">Quantity</th>
<th class="text-center">Prize</th>
<th class="text-center">TotalPrize</th>
<th class="text-center">Edit</th>
<th class="text-center">Delete</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td class="id">@item.ProductId</td>
<td>@item.ProductName</td>
<td>@item.Quantity</td>
<td>@item.Prize</td>
@*@double totalPrize = @item.Quantity * @item.Prize*@
<td></td>
<td class="text-center"><a href="@Url.Action("Edit", "Shopping", new { @productId = @item.ProductId })" class="openDialog-Edit"><i class="glyphicon glyphicon-edit" style="cursor: pointer;"></i>Edit</a></td>
<td class="text-center"><a href="@Url.Action("DeleteProduct", "Shopping", new { @productId = @item.ProductId })"><i class="glyphicon glyphicon-trash" style="cursor: pointer;"></i>Delete</a></td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="modal fade" id="addmodel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Product Details</h4>
</div>
<div class="divForAdd">
</div>
</div>
</div>
</div>
<div class="modal fade" id="editmodel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Update Product Details</h4>
</div>
<div class="divForCreate">
</div>
</div>
</div>
</div>
</div>
2. Add.cshtml
@model ShoppingApplication.ProductDetail
@using (Html.BeginForm("Add", "Shopping", FormMethod.Post))
{
<div class="modal-body">
<div class="page-header">
<h1 class="co">Add New Product </h1>
</div>
@*<div class="row">*@
<div class="row">
<div class="col-md-1">
ProductName
</div>
<div class="col-md-2">
@Html.TextBoxFor(m => m.ProductName, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"> </div>
<div class="row">
<div class="col-md-1">
Quantity
</div>
<div class="col-md-2">
@Html.TextBoxFor(m => m.Quantity, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"> </div>
<div class="row">
<div class="col-md-1">
Prize
</div>
<div class="col-md-2">
@Html.TextAreaFor(m => m.Prize, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"> </div>
@*</div>*@
<div class="clearfix"> </div>
<div class="row">
<button type="button" id ="close" class="btn btn-default" data-dismiss="modal" onclick="window.location.href('@Url.Action("Index","Shopping")')" >Close</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
}
3. Edit.cshtml
@model ShoppingApplication.ProductDetail
@using (Html.BeginForm("Edit", "Shopping", FormMethod.Post))
{
<div class="modal-body">
<div class="row">
<div class="col-md-4">
ProductId
</div>
<div class="col-md-8">
@Html.TextBoxFor(m => m.ProductId, new { @class = "form-control",@readonly="readonly" })
</div>
</div>
<div class="row">
<div class="col-md-4">
ProductName
</div>
<div class="col-md-8">
@Html.TextBoxFor(m => m.ProductName, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"> </div>
<div class="row">
<div class="col-md-4">
Quantity
</div>
<div class="col-md-8">
@Html.TextBoxFor(m => m.Quantity, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"> </div>
<div class="row">
<div class="col-md-4">
Prize
</div>
<div class="col-md-8">
@Html.TextAreaFor(m => m.Prize, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"> </div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
}
App.js File :
$(document).ready(function () {
$('#add').click(function (event) {
event.preventDefault();
$.get(this.href, function (response) {
$('.divForAdd').html(response);
});
$('#addmodel').modal({
backdrop: 'static',
}, 'show');
});
$('.openDialog-Edit').click(function (event) {
event.preventDefault();
$.get(this.href, function (response) {
$('.divForCreate').html(response);
});
$('#editmodel').modal({
backdrop: 'static',
}, 'show');
});
$('#basic-addon1').click(function (e) {
var name = $('#search-box').val();
e.preventDefault(); // <------------------ stop default behaviour of button
$.ajax({
url: "/Home/Index",
type: "POST",
data: { 'Name': name },
success: function (data) {
$('#div-main').html(data);
},
error: function () {
alert("An error has occured!!!");
}
});
});
});
Make Sure you Add below Script for UI and Click events to be worked ...
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.css" rel="stylesheet" />
<script src="~/Scripts/jquery-2.1.1.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script src="~/Scripts/App.js"></script>
Below Are the Output Screen :
Let me know in comments if you find any problem while implementing the same :)
Happy Coding










Comments
Post a Comment