Sunday, 11 May 2014

insert update delete in mvc

No comments
create model class

 public class Portal
    {
        public int uid
        { get; set; }
        public string fname
        { get; set; }
        public string lname
        { get; set; }
        public string email
        { get; set; }
        public string address
        { get; set; }
        public string phone
        { get; set; }
        public string company
        { get; set; }
        public string designation
        { get; set; }
    }


 create controler

using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using firstmvc.Models;



public class TestController : Controller
    {
        StarBusEntities entity = new StarBusEntities();
        public ActionResult Index()
        {           
            return View();
        }
       
        [HttpGet]
        public ActionResult Insert()
        {
            Portal p1 = new Portal();
            return View(p1);
        }

        [HttpPost]
        public ActionResult Insert(Portal p1)
        {
            mvc_test mv = new mvc_test();
            mv.uid=  p1.uid ;
            mv.fname=p1.fname ;
            mv.lname=p1.lname;
            mv.email=p1.email ;
            entity.mvc_test.AddObject(mv);
            entity.SaveChanges();
            return View();
        }

        public ActionResult showEmp()
        {
            List<Portal> l1 = new List<Portal>();
            var data = from d in entity.mvc_test
                       select d;
            foreach (var p1 in data)
            {
                Portal mv = new Portal();
                mv.uid = p1.uid;
                mv.fname = p1.fname;
                mv.lname = p1.lname;
                mv.email = p1.email;
                l1.Add(mv);
            }
            return View(l1);
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var p1 = entity.mvc_test.Single(i => i.uid == id);
            Portal mv = new Portal();
            mv.uid = p1.uid;
            mv.fname = p1.fname;
            mv.lname = p1.lname;
            mv.email = p1.email;
            return View(mv);
        }
        [HttpPost]
        public ActionResult Edit(Portal p1, int id)
        {
            var mv = entity.mvc_test.Single(i => i.uid == id);
            mv.uid = p1.uid;
            mv.fname = p1.fname;
            mv.lname = p1.lname;
            mv.email = p1.email;
            entity.SaveChanges();
            return RedirectToAction("showEmp");
       
        }

        [HttpGet]
        public ActionResult Detail(int id)
        {
            var p1 = entity.mvc_test.Single(i => i.uid == id);
            Portal mv = new Portal();
            mv.uid = p1.uid;
            mv.fname = p1.fname;
            mv.lname = p1.lname;
            mv.email = p1.email;
            return View(mv);
        }

        [HttpGet]
        public ActionResult Delete(int id)
        {
            var p1 = entity.mvc_test.Single(i => i.uid == id);
            Portal mv = new Portal();
            mv.uid = p1.uid;
            mv.fname = p1.fname;
            mv.lname = p1.lname;
            mv.email = p1.email;
 
           return View(mv);

        }
        [HttpPost]
        public ActionResult Delete(Portal p,int id)
        {
            var p1 = entity.mvc_test.Single(i => i.uid == id);           
            entity.mvc_test.DeleteObject(p1);
            entity.SaveChanges();
            return RedirectToAction("showEmp");
        }
    }