using Practiceajax.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Practiceajax.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult ViewAll()
{
using (DBEntities db = new DBEntities())
{
var emp = db.Emps.ToList();
return Json(new { data = emp, status = "success" }, JsonRequestBehavior.AllowGet);
}
}
[HttpGet]
public ActionResult AddEdit(int id=0)
{
Emp emp = new Emp();
if (id != 0)
{
using(DBEntities db = new DBEntities())
{
emp = db.Emps.Find(id);
}
}
return Json(new { data = emp, status = "success" }, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult AddEdit(Emp emp)
{
using(DBEntities db = new DBEntities())
{
if (emp.Id == 0)
{
db.Emps.Add(emp);
db.SaveChanges();
}
else
{
db.Entry(emp).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
}
return Json(new { data = emp, status = "success" }, JsonRequestBehavior.AllowGet);
}
public ActionResult Delete(int Id)
{
using(DBEntities db = new DBEntities())
{
var emp = db.Emps.Find(Id);
db.Emps.Remove(emp);
db.SaveChanges();
return Json(new { data = "Deleted", status = "success" }, JsonRequestBehavior.AllowGet);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Practiceajax.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult ViewAll()
{
using (DBEntities db = new DBEntities())
{
var emp = db.Emps.ToList();
return Json(new { data = emp, status = "success" }, JsonRequestBehavior.AllowGet);
}
}
[HttpGet]
public ActionResult AddEdit(int id=0)
{
Emp emp = new Emp();
if (id != 0)
{
using(DBEntities db = new DBEntities())
{
emp = db.Emps.Find(id);
}
}
return Json(new { data = emp, status = "success" }, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult AddEdit(Emp emp)
{
using(DBEntities db = new DBEntities())
{
if (emp.Id == 0)
{
db.Emps.Add(emp);
db.SaveChanges();
}
else
{
db.Entry(emp).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
}
return Json(new { data = emp, status = "success" }, JsonRequestBehavior.AllowGet);
}
public ActionResult Delete(int Id)
{
using(DBEntities db = new DBEntities())
{
var emp = db.Emps.Find(Id);
db.Emps.Remove(emp);
db.SaveChanges();
return Json(new { data = "Deleted", status = "success" }, JsonRequestBehavior.AllowGet);
}
}
}
}
Post a Comment
If you have any doubt please let me know