REFERENCE CRUD
by Ninay is Unknown
HTML
Index.cshtml (View)
<!-- @{
ViewBag.Title = "EmployeeList";
}
<h2>Employee List</h2>
<a class="btn btn-success" style="margin-bottom:10px;" onclick="Popupform('@Url.Action("AddOrEdit","Employee")')">+ Add New</a>
<table id="employeetable" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Salary</th>
<th></th>
</tr>
</thead>
</table>
<link href="~/Content/DataTables/css/dataTables.bootstrap.min.css" rel="stylesheet" />
@section Scripts{
<script type="text/javascript" src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="~/Scripts/DataTables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript">
var Popup, dataTable;
$(document).ready(function () {
dataTable = $('#employeetable').DataTable({
"ajax": {
"url": "/Employee/GetData",
"type": "GET",
"data": "json"
},
"columns": [
{ "data": "Name" },
{ "data": "Position" },
{ "data": "Office" },
{ "data": "Age" },
{ "data": "Salary" },
{ "data": "EmployeeID", "render": function (data) {
return "<a class='btn btn-default btn-sm' onclick=Popupform('@Url.Action("AddOrEdit","Employee")/"+ data +"')>Edit</a> <a class='btn btn-info btn-sm' style='margin-left:5px;' onclick=Delete("+data+")>Delete</a>";
},
"orderable": false,
"searchable": false,
"width":"150px"
}
],
"language": {
"emptyTable": "No data found, please click on <b>Add...
CSS
/* make sure DBModels the name of your entitymodels */
/* Reference Bundles */
/* bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-1.12.4.min.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate.min.js", "~/Scripts/jquery.validate.unobtrusive.min.js"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js"));
bundles.Add(new ScriptBundle("~/bundles/jquery-ui").Include(
"~/Scripts/jquery-ui-1.12.1.min.js", "~/Scripts/notify.min.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/site.css", "~/Content/bootstrap.css", "~/Content/themes/base/jquery-ui.min.css" )); */
JavaScript
/* Employee Controller */
/* using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication14.Models;
namespace WebApplication14.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Index()
{
return View();
}
public ActionResult GetData()
{
using (DBModel db = new DBModel())
{
List<Employee> empList = db.Employees.ToList<Employee>();
return Json(new { data = empList }, JsonRequestBehavior.AllowGet);
}
}
[HttpGet]
public ActionResult AddOrEdit(int id = 0)
{
if( id == 0)
return View(new Employee());
else
{
using (DBModel db = new DBModel())
{
return View(db.Employees.Where(x => x.EmployeeID == id).FirstOrDefault<Employee>());
}
}
}
[HttpPost]
public ActionResult AddOrEdit(Employee emp)
{
using (DBModel db = new DBModel())
{
if (emp.EmployeeID == 0) {
db.Employees.Add(emp);
db.SaveChanges();
return Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
}
else
{
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
return Json(new { success = true, message = "Update Successfully" }, JsonRequestBehavior.AllowGet);
}
}
}
[HttpPost]
public ActionResult Delete(int id)
{
using (DBModel db = new DBModel())
{
Employee emp = db.Employees.Where(x=> x.EmployeeID ==...