Wednesday, December 2, 2015

EmployeeController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCEntity.Context;
using MVCEntity.Models;
using System.Net;

namespace MVCEntity.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/
        private DatabaseContext db = new DatabaseContext();

        public ActionResult Index()
        {
            return View(db.Employees.ToList());
        }

        //
        // GET: /Employee/Details/5
        [HttpGet]
        public ActionResult Details(int? id)
        {
            if (id == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            Employee employee = db.Employees.Find(id);
            if (employee == null)
                return HttpNotFound();
            return View(employee);
        }

        //
        // GET: /Employee/Create
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(Employee employee)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Employees.Add(employee);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Employee/Edit/5

        [HttpGet]
        public ActionResult Edit(int? id)
        {
            if (id == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            Employee employee = db.Employees.Find(id);
            if (employee == null)
                return HttpNotFound();


            return View(employee);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        [ActionName("Edit")]
        public ActionResult Edit(Employee employee)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(employee).State = System.Data.EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(employee);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Employee/Delete/5

        public ActionResult Delete(int? id)
        {
            if (id == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            Employee employee = db.Employees.Find(id);
            if (employee == null)
            return HttpNotFound();

            return View(employee);
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost]
        [ActionName("Delete")]
        public ActionResult Delete_post(int? id)
        {
            try
            {
                // TODO: Add delete logic here
                Employee employee = new Employee();
                if (ModelState.IsValid)
                {
                    if (id == null)
                        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                    employee = db.Employees.Find(id);
                    if (employee == null)
                        return HttpNotFound();

                    db.Employees.Remove(employee);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(employee);
            }
            catch
            {
                return View();
            }
        }
    }
}

0 comments:

Post a Comment

Popular Posts

Recent Posts

Text Widget

Powered by Blogger.

Find Us On Facebook

Flickr Images

Recent Posts

Video Of Day

Random Posts

Facebook

Random Posts

Advertising

Popular Posts