Simple CRUD [Insert, Update, Delete]
Here I have explain how to create simple insert update delete using asp.net MVC, So simply follow below steps and see the result.
Step 1
Let's create model and give name CompanyMaster and Create table in database as shown as model fields
Model
using System.Data.Entity;
using
System.ComponentModel.DataAnnotations;
using
System.ComponentModel.DataAnnotations.Schema;
namespace MyProject.Models
{
[Table("CompanyMaster")]
public class CompanyMaster
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int CMPId { get; set; }
public string CMPEnc { get; set; }
[Display(Name = "Company Name")]
public string CMPName { get; set; }
}
public class CompanyMasterContext : DbContext
{
public CompanyMasterContext() : base("ConString_Name")
{
}
public DbSet<CompanyMaster>
CompanyMaster { get; set; }
}
}
Step 2
Now add new controller and give name "AdminCompanyController",And add all methods as shown as in below controller. it's Area controller but we can use simple controller also
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyProject.Models;
using System.Data.Entity;
namespace MyProject.Areas.Admin.Controllers
{
public class AdminCompanyController : Controller
{
CompanyMasterContext
_companyContext = new
CompanyMasterContext();
public ActionResult Index()
{
if (Session["AdminId"] != null)
{
var list =
_companyContext.CompanyMaster.ToList();
return View(list);
}
else
{
return RedirectToAction("Login", "Admin", new { area = "Admin" });
}
}
public ActionResult Create()
{
if (Session["AdminId"] != null)
{
return View();
}
else
{
return RedirectToAction("Login", "Admin", new { area = "Admin" });
}
}
[HttpPost]
public ActionResult Create(CompanyMaster
companyMaster)
{
if (Session["AdminId"] != null)
{
if (ModelState.IsValid)
{
_companyContext.CompanyMaster.Add(companyMaster);
_companyContext.SaveChanges();
companyMaster.CMPEnc =
enc.Encrypt(companyMaster.CMPId);
_companyContext.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(companyMaster);
}
}
else
{
return RedirectToAction("Login", "Admin", new { area = "Admin" });
}
}
public ActionResult Edit(string id)
{
if (Session["AdminId"] != null)
{
string myId = id.Substring(1, 6);
int ID = enc.Decrypt(myId);
var update =
_companyContext.CompanyMaster.Where(b => b.CMPId.Equals(ID)).FirstOrDefault();
return View(update);
}
else
{
return RedirectToAction("Login", "Admin", new { area = "Admin" });
}
}
[HttpPost]
public ActionResult Edit(CompanyMaster
companyMaster)
{
if (Session["AdminId"] != null)
{
if (ModelState.IsValid)
{
_companyContext.Entry(companyMaster).State = EntityState.Modified;
_companyContext.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(companyMaster);
}
}
else
{
return RedirectToAction("Login", "Admin", new { area = "Admin" });
}
}
public ActionResult Details(string id)
{
if (Session["AdminId"] != null)
{
string myId = id.Substring(1, 6);
int ID = enc.Decrypt(myId);
var detail =
_companyContext.CompanyMaster.Where(b =>
b.CMPId.Equals(ID)).FirstOrDefault();
return View(detail);
}
else
{
return RedirectToAction("Login", "Admin", new { area =
"Admin" });
}
}
[HttpPost]
public ActionResult Delete(string id)
{
if (Session["AdminId"] != null)
{
string myID = id.Substring(1, 6);
int ID = enc.Decrypt(myID);
var del =
_companyContext.CompanyMaster.Where(b =>
b.CMPId.Equals(ID)).FirstOrDefault();
_companyContext.CompanyMaster.Remove(del);
_companyContext.SaveChanges();
return RedirectToAction("Index");
}
else
{
return RedirectToAction("Login", "Admin", new { area = "Admin" });
}
}
}
}
Step 3
It's time to add Views for all methods like List, Create, Edit, Detail and Detail
Add view like below, See below views I have created all of them
View:
Index [ List ]
@model IEnumerable<MyProject.Models.CompanyMaster>
@{
ViewBag.Title = "Company";
Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<div class="entry">
<h4>Company List</h4>
<div class="row nt-table
table-responsive">
<table class="">
<tr>
<th>
<div class="tbl-srNo">
Sr
No
</div>
</th>
<th>
<div class="tbl-name">
@Html.DisplayNameFor(model => model.CMPName)
</div>
</th>
<th colspan="3">
<div class=" tbl-contact">
Action
</div>
</th>
</tr>
@{
int SrNo = 0;
}
@foreach (var item in Model)
{
<tr>
@{
SrNo++;
}
<td>@SrNo</td>
<td>
@Html.DisplayFor(modelItem
=> item.CMPName)
</td>
<td style="width:40px">
<a href="~/Admin/AdminArea/Edit/@item.CMPEnc" title="Edit"><i class="icofont icofont-open-eye"></i></a>
</td>
<td style="width:40px">
<a href="~/Admin/AdminArea/Details/@item.CMPEnc" title="Details"><i class="icofont icofont-info-square"></i></a>
</td>
<td style="width:6%">
@using (Html.BeginForm("Delete", "AdminCompany",
FormMethod.Post, new { id =
item.CMPEnc, @class = "myForm" }))
{
<input type="hidden" value="@item.CMPEnc" name="id" />
var myModal = "myModal" + item.CMPEnc;
<a href="" type="button" class="btn
btn-sqr2 form-delete" data-toggle="modal" data-target="#@myModal" title="Delete"><i class="icofont icofont-basket"></i></a>
<!--Modal-->
<div class="modal fade" id="@myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-sm" role="document" style="position: initial;width: 410px; margin-top:140px">
<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>
<h3 class="modal-title" id="myModalLabel" style="color: #f20000;">Confirm Delete</h3>
</div>
<div class="modal-body">
Are
you sure you want to delete : <span><b style="color: red;font-size: 16px;"> @item.CMPName </b>?</span>
</div>
<div class="modal-footer">
<button type="button" class="btn
btn-default" data-dismiss="modal">Close</button>
<input type="submit" value="Delete" class="btn
btn-danger" />
</div>
</div>
</div>
</div>
}
</td>
</tr>
}
</table>
</div>
</div>
Create [ Add Company ]
@model MyProject.Models.CompanyMaster
@{
ViewBag.Title = "Create";
Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="entry">
<h4>Add Company <a href="~/Admin/AdminCompany" class="btn
btn-info" style="float:right;margin:-7px">Company List</a></h4>
@Html.ValidationSummary(true, "", new { @class
= "text-danger" })
<div class="row">
<div class="col-md-4 form-group">
<div class="entry-1">
@Html.LabelFor(model
=> model.CMPName, htmlAttributes: new { @class = "" })
@Html.EditorFor(model
=> model.CMPName, new {
htmlAttributes = new { @class
= "form-control" } })
@Html.ValidationMessageFor(model
=> model.CMPName, "", new { @class
= "text-danger" })
</div>
</div>
<div class="col-md-8">
<div class="entry-1">
<button type="submit" class="btn
btn-success" style="background-color: #218838;">Submit</button>
</div>
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Edit [ Update company ]
@model MyProject.Models.CompanyMaster
@{
ViewBag.Title = "Edit";
Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="entry">
<h4>Edit Company</h4>
@Html.ValidationSummary(true, "", new { @class
= "text-danger" })
@Html.HiddenFor(model
=> model.CMPId)
@Html.HiddenFor(model
=> model.CMPEnc)
<div class="row">
<div class="col-md-4 form-group">
<div class="entry-1">
@Html.LabelFor(model
=> model.CMPName, htmlAttributes: new { @class = "" })
@Html.EditorFor(model
=> model.CMPName, new {
htmlAttributes = new { @class
= "form-control" } })
@Html.ValidationMessageFor(model
=> model.CMPName, "", new { @class
= "text-danger" })
</div>
</div>
<div class="col-md-8">
<div class="entry-1">
<button type="submit" class="btn
btn-success" style="background-color: #218838;">Submit</button>
<a class="btn btn-success" href="~/Admin/AdminCompany" style="background-color: #218838; margin-top:35px;">Back</a>
</div>
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Details [ Company details ]
@model MyProject.Models.CompanyMaster
@{
ViewBag.Title = "Details";
Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<div class="entry">
<h4>Company Details</h4>
@Html.ValidationSummary(true, "", new { @class
= "text-danger" })
@Html.HiddenFor(model => model.CMPId)
@Html.HiddenFor(model => model.CMPEnc)
<div class="row">
<div class="col-md-4 form-group">
<div class="entry-1 entry-view">
@Html.LabelFor(model
=> model.CMPName, htmlAttributes: new { @class = "" })
@Html.EditorFor(model
=> model.CMPName, new {
htmlAttributes = new { @class
= "", @disabled = "disabled" } })
@Html.ValidationMessageFor(model
=> model.CMPName, "", new { @class
= "text-danger" })
</div>
</div>
<div class="col-md-8">
<div class="entry-1">
<a class="btn btn-success" href="~/Admin/AdminCompany" style="background-color: #218838; margin-top:35px;">Back</a>
</div>
</div>
</div>
</div>
Let's run the project and see the result