Database code
create database mvc
create table tbl_products
(
id int primary key identity(1,1),
product_name varchar(100),
quantity int
)
insert into tbl_products values ('rice',100)
create procedure get_products
as begin
select * from tbl_products
end
go
create procedure insert_products
(
@product_name varchar(100),
@quantity int
)
as begin
insert into tbl_products values
(@product_name,@quantity)
end
insert_products 'dhall',50
edit_products 1,'salt',50
create procedure edit_products
(
@id int,
@product_name varchar(100),
@quantity int
)
as begin
update tbl_products set product_name=@product_name
, quantity=@quantity where id =@id
end
create procedure delete_products
(
@id int
)
as begin
delete from tbl_products where id =@id
end
go
create procedure get_products_id
(@id int
)
as begin
select * from tbl_products where id=@id
end Model Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ArunDepartmentalStore.Models
{
public class tbl_products
{
public int id { get; set; }
public string product_name { get; set; }
public int quantity { get; set; }
}
} Controller Code
using ArunDepartmentalStore.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ArunDepartmentalStore.Controllers
{
public class tbl_productsController : Controller
{
string constr = "Data Source=.\\sqlexpress;Initial Catalog=mvc;Integrated Security =true";
// GET: tbl_products
public ActionResult Index()
{
List tbl_products_obj = new List();
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("get_products", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while(sdr.Read())
{
tbl_products_obj.Add(new tbl_products
{
id = Convert.ToInt32( sdr["id"]),
product_name = Convert.ToString(sdr["product_name"]),
quantity = Convert.ToInt32(sdr["quantity"])
}
);
}
con.Close();
return View(tbl_products_obj);
}
// GET: tbl_products/Details/5
public ActionResult Details(int id,tbl_products tbl_products_obj)
{
SqlConnection con = new SqlConnection(constr);
string query = "get_products_id " + id;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
tbl_products_obj = new tbl_products
{
id = Convert.ToInt32(sdr["id"]),
product_name = Convert.ToString(sdr["product_name"]),
quantity = Convert.ToInt32(sdr["quantity"])
};
}
con.Close();
return View(tbl_products_obj);
}
// GET: tbl_products/Create
public ActionResult Create()
{
return View();
}
// POST: tbl_products/Create
[HttpPost]
public ActionResult Create(tbl_products tbl_productobj)
{
try
{
SqlConnection con = new SqlConnection(constr);
string query = "insert_products '" + tbl_productobj.product_name + "'," + tbl_productobj.quantity;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: tbl_products/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: tbl_products/Edit/5
[HttpPost]
public ActionResult Edit(int id, tbl_products tbl_productobj)
{
try
{
SqlConnection con = new SqlConnection(constr);
string query = "edit_products " + id +",'" + tbl_productobj.product_name + "'," + tbl_productobj.quantity;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: tbl_products/Delete/5
public ActionResult Delete(int id,tbl_products tbl_products_obj)
{
SqlConnection con = new SqlConnection(constr);
string query = "get_products_id " + id;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
tbl_products_obj = new tbl_products
{
id = Convert.ToInt32(sdr["id"]),
product_name = Convert.ToString(sdr["product_name"]),
quantity = Convert.ToInt32(sdr["quantity"])
} ;
}
con.Close();
return View(tbl_products_obj);
}
// POST: tbl_products/Delete/5
[HttpPost]
public ActionResult Delete(int id)
{
try
{
SqlConnection con = new SqlConnection(constr);
string query = "delete_products " + id ;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
} View Code
Index.cshtml
Index.cshtml
@model IEnumerable
@{
ViewBag.Title = "Index";
}
Index
@Html.ActionLink("Create New", "Create")
@Html.DisplayNameFor(model => model.product_name)
@Html.DisplayNameFor(model => model.quantity)
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.product_name)
@Html.DisplayFor(modelItem => item.quantity)
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
}
create.cshtml
create.cshtml
@model ArunDepartmentalStore.Models.tbl_products
@{
ViewBag.Title = "Create";
}
Create
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
tbl_products
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.product_name, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.product_name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.product_name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.quantity, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.quantity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.quantity, "", new { @class = "text-danger" })
}
@Html.ActionLink("Back to List", "Index")
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
} edit.cshtml
edit.cshtml
@model ArunDepartmentalStore.Models.tbl_products
@{
ViewBag.Title = "Edit";
}
Edit
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
tbl_products
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.id)
@Html.LabelFor(model => model.product_name, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.product_name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.product_name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.quantity, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.quantity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.quantity, "", new { @class = "text-danger" })
}
@Html.ActionLink("Back to List", "Index")
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
} Details.cshtml
Details.cshtml
@model ArunDepartmentalStore.Models.tbl_products
@{
ViewBag.Title = "Details";
}
Details
tbl_products
@Html.DisplayNameFor(model => model.product_name)
@Html.DisplayFor(model => model.product_name)
@Html.DisplayNameFor(model => model.quantity)
@Html.DisplayFor(model => model.quantity)
@Html.ActionLink("Edit", "Edit", new { id = Model.id }) |
@Html.ActionLink("Back to List", "Index")
Delete.cshtml
Delete.cshtml
@model ArunDepartmentalStore.Models.tbl_products
@{
ViewBag.Title = "Delete";
}
Delete
Are you sure you want to delete this?
tbl_products
@Html.DisplayNameFor(model => model.product_name)
@Html.DisplayFor(model => model.product_name)
@Html.DisplayNameFor(model => model.quantity)
@Html.DisplayFor(model => model.quantity)
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
|
@Html.ActionLink("Back to List", "Index")
}