how to create simple mvc CRUD in asp.net using json
how to create simple mvc CRUD in asp.net using json
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
sachin shethPosted Feb 23, 2016, 1:08 PM
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcCrud.Controllers
{
public class HomeController : Controller
{
public TraineeDemoEntities edc = new TraineeDemoEntities();
public ActionResult index()
{
return RedirectToAction("Employee");
}
public ActionResult Employee()
{
return View();
}
public string SaveEmployee(decimal id, string Name, string Address)
{
string msg = "";
if (id == 0)
{
var e1 = new Student();
e1.Name = Name;
e1.Address = Address;
e1.Isactive = 1;
edc.Students.AddObject(e1);
msg = "Insert Successfully ";
edc.SaveChanges();
}
else
{
Student s1 = edc.Students.SingleOrDefault(t => t.Sid == id);
s1.Name = Name;
s1.Address = Address;
s1.Isactive = 1;
msg = "Update Successfully";
edc.SaveChanges();
}
return msg;
}
public JsonResult EditStudent(decimal id)
{
var objstud = edc.Students.Where(t => t.Sid == id).ToList();
return Json(objstud, JsonRequestBehavior.AllowGet);
}
public JsonResult BindStudent()
{
var objstud = edc.Students.Where(t => t.Isactive == 1).ToList();
return Json(objstud, JsonRequestBehavior.AllowGet);
}
public string DelStudent(int id)
{
string flg = "";
try
{
Student objstu = edc.Students.Where(t => t.Sid == id).SingleOrDefault();
if (objstu != null)
{
objstu.Isactive = 0;
edc.SaveChanges();
flg = "true";
}
}
catch (Exception ex)
{
throw ex;
}
return flg;
}
}
}
sachin shethPosted Feb 23, 2016, 1:07 PM
Edit
Name
Address
Delete
sachin shethPosted Feb 23, 2016, 12:54 PM