http://diwakarko.blogspot.com (दिवाकरको ब्लग)

MVC Complete Add/Edit/Delete/Details Controller with Entity FrameWork

No comments
public class genreController : Controller
    {
        //
        // GET: /genre/
        MVCBookStoreEntities db = new MVCBookStoreEntities();

        public ActionResult Index()
        {
            //var model = db.genres.ToList();
            //return View(model);          

            List gen =new List();
            gen=db.genres.ToList();
            var model=from g in gen
                      orderby g.GenreId descending
                      where g.Status==true
                      select g;
            return View(model);
        }


        //
        // GET: /genre/Details/5

        public ActionResult Details(int id)
        {
            var model = db.genres.Find(id);
            return View(model);
        }

        //
        // GET: /genre/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /genre/Create

        [HttpPost]
        public ActionResult Create(Genre addgen)
        // public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                //return RedirectToAction("Index");
                db.Entry(addgen).State = EntityState.Added;
                //db.genres.Add(addgen);
                db.SaveChanges();
                return RedirectToAction("Index");


            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /genre/Edit/5

        public ActionResult Edit(int id)
        {
            var model=db.genres.Find(id);
            return View(model);
        }

        //
        // POST: /genre/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Genre editgen)
        //public ActionResult Edit(int id, FormCollection collection)
        {
                try
                {
                    if (ModelState.IsValid)
                    {
                        // TODO: Add update logic here
                        editgen.GenreId = id;
                        db.Entry(editgen).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                    else {
                        return RedirectToAction("Index");
                    }
                }
                catch
                {
                    return View();
                }
           
        }

        //
        // GET: /genre/Delete/5

        public ActionResult Delete(int id)
        {
            var model = db.genres.Find(id);
            return View(model);
        }

        //
        // POST: /genre/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Genre deletegen)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // TODO: Add update logic here
                    deletegen.GenreId = id;
                    db.Entry(deletegen).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }
    }

No comments :

Post a Comment