Monday, 7 November 2016

Include and Exclude Properties for Update in MVC

Include and Exclude Properties
They help in preventing updates in MVC. A read only field can be easily updated using tools like fiddler. To prevent such unintended updates we can use Include and Exclude Properties.

This is What we want to Achieve

 To Reach to the Index screen of above Screen Shot. Follow below link
2. Adding Create New option
3. Updating and Deleting Record in MVC

Step 1 : Make the following change in Edit Post back request in Employee Controller

        [HttpPost]
        [ActionName("Edit")]
        public ActionResult Edit_Post(int id)
        {
            EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
            Employee employee = employeeBuisnessLayer.Employees.Single(emp => emp.EmpID == id);
            UpdateModel(employee, null, null, new string[] { "EmpName"});//Exclude List

            if (ModelState.IsValid)
            {
                employeeBuisnessLayer.Employee_Update(employee);
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }

        }


OR

        [HttpPost]
        [ActionName("Edit")]
        public ActionResult Edit_Post(int id)
        {
            EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
            Employee employee = employeeBuisnessLayer.Employees.Single(emp => emp.EmpID == id);
            UpdateModel(employee, new string[] { "EmpDesignation" });//Include List

            if (ModelState.IsValid)
            {
                employeeBuisnessLayer.Employee_Update(employee);
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

OR

        [HttpPost]
        [ActionName("Edit")]
        public ActionResult Edit_Post([Bind(Include = "EmpID,EmpDesignation")] Employee employee)
        {
            EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
            employee.EmpName = employeeBuisnessLayer.Employees.Single(emp => emp.EmpID == employee.EmpID).EmpName;
            if (ModelState.IsValid)
            {
                employeeBuisnessLayer.Employee_Update(employee);
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

OR

        [HttpPost]
        [ActionName("Edit")]
        public ActionResult Edit_Post([Bind(Exclude = "EmpName")] Employee employee)
        {
            EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
            employee.EmpName = employeeBuisnessLayer.Employees.Single(emp => emp.EmpID == employee.EmpID).EmpName;
            if (ModelState.IsValid)
            {
                employeeBuisnessLayer.Employee_Update(employee);
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

OR

In Employee Class
    public interface IEmployee
    {
        int EmpID { get; set; }
        string EmpDesignation { get; set; }
    }
    public class Employee : IEmployee
    {
        public int EmpID { get; set; }
        [Required]
        public string EmpName { get; set; }
        [Required]
        public string EmpDesignation { get; set; }

    }

In Employee Controller
        [HttpPost]
        [ActionName("Edit")]
        public ActionResult Edit_Post(int id)
        {
            EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
            Employee employee = employeeBuisnessLayer.Employees.Single(emp => emp.EmpID == id);
            UpdateModel<IEmployee>(employee);
            if (ModelState.IsValid)
            {
                employeeBuisnessLayer.Employee_Update(employee);
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

Step 2 : Run the Application





0 comments:

Post a Comment