Skip to content

How to create simple mvc CRUD in asp.net using json

sachin sheth

Asked Feb 23, 2016, 7:51 AM EST3 answers
Share
LinkedInX

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    http://www.c-sharpcorner.com/UploadFile/cd7c2e/implement-insert-update-and-delete-functionality/ http://kushonline.blogspot.in/2015/02/insert-delete-update-retrieve-data-with.html
    0
  • 0





















    Student Details


    Student Name:



    Address:
























    Edit

    Name

    Address

    Delete

    0
  • 0
    using System;
    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;
    }
    }
    }
    0