Thursday, 31 October 2013

Add,Edit,delete functionality in MVC (controller Detail)

AccountController

public class AccountController : Controller
    {
        //
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (new AccountRepository().AuthenticateUser(model.UserName, model.Password))
                    return RedirectToLocal(returnUrl);
                else
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
            return View(model);
        }

        //
        // POST: /Account/LogOff

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            WebSecurity.Logout();

            return RedirectToAction("Index", "Home");
        }
}



Employee Controller

using MVCDemo.Repository;
using MVCDemo.Models;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MVCDemo.Controllers
{
    public class EmployeeController : Controller
    {
        TestEntities _db = new TestEntities();
        public ActionResult ListEmployees()
        {
            var Emp = new EmployeeRepository().GetAllEmployee();
            return View(Emp);
        }


        public ActionResult Add()
        {
            fillGender(ViewBag, string.Empty);
            FillCountryDropDown(ViewBag, "--Select Country--");
            FillOccupation(ViewBag, "--Select Occupation--");
            return View();
        }

        public void fillGender(dynamic Viewbag, string SelectVal)
        {
            var Gender = new SelectList(new[]
                                            {   new {Value = "", Text = "--Select--"},
                                                new {Value = "0",Text = "Male"},
                                                new {Value = "1",Text = "FeMale"},
                                            }.ToList(), "Value", "Text", SelectVal);
            Viewbag.Gender = Gender;
        }

        public void FillOccupation(dynamic Viewbag, string SelectVal)
        {  

            var Occupation = new SelectList(new[]
                                                {
                                                    new {Value = "",Text="--Select Occupation--"},
                                                    new {Value ="Student",Text="Student"},
                                                    new {Value ="Job",Text="Job"},
                                                    new {Value ="Bussiness",Text="Bussiness"},
                                                }.ToList(), "Value", "Text",SelectVal);
            ViewBag.Occupation = Occupation;
        }

        public void FillCountryDropDown(dynamic Viewbag, string SelectVal)
        {
             var Country = (from c in _db.CountryMasters select c);
                         //orderby d.Name
                         //select d;
             ViewBag.Country = new SelectList(Country, "CountryId", "CountryName", SelectVal);
        }

        public ActionResult GetCity(int CountryId)
        {
            var City = (from c in _db.CityMasters where c.CountryId == CountryId select c);
            return Json(new SelectList(City.ToArray(), "CityId", "CityName"), JsonRequestBehavior.AllowGet);
           
        } 

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(EmployeeMaster emp)
        {
            if (new EmployeeRepository().SaveEmployee(emp))
            {
                 string strPath = Server.MapPath("~/Uploads");

                DirectoryInfo dir = new DirectoryInfo(strPath);
                if (!dir.Exists)
                {
                    dir.Create();
                } 


                var postedFileBase = Request.Files[0];
                if (postedFileBase != null && postedFileBase.FileName.Length > 0)
                {
                    string pic = System.IO.Path.GetFileName(postedFileBase.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("~/Uploads"), pic);
                    // file is uploaded
                    postedFileBase.SaveAs(path);
                }
                return RedirectToAction("ListEmployees");
            }
            return View();
        }


        public ActionResult Edit(int id = 0)
        {
            var emp = new EmployeeRepository().GetEmpByID(id);
            FillCountryDropDown(ViewBag, emp.CountryId.ToString());
            fillGender(ViewBag, emp.Gender);
            FillOccupation(ViewBag, emp.Occupation);
            ViewBag.CityId = emp.CityId;
            ViewBag.Occu = emp.Occupation;
            return View(emp);
        }

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(EmployeeMaster emp)
        {
            if (ModelState.IsValid)
            {
                if (new EmployeeRepository().SaveEmployee(emp))
                {
                    return RedirectToAction("ListEmployees");
                }
            }
            FillCountryDropDown(ViewBag, emp.CountryId.ToString());
            fillGender(ViewBag, emp.Gender);
            FillOccupation(ViewBag, emp.Occupation);
            return View(emp);
        }

        public ActionResult DeleteEmp(int id = 0)
        {
            if (new EmployeeRepository().DeleteEmp(id))
                return RedirectToAction("ListEmployees");
            return View();
        }

    }
}
 


No comments:

Post a Comment