30 May 2018

How to insert update delete in mvc using repository pattern


How to add Controller:-


Step3:-


Step4:-How to add repository folder the create the the class in folder.


Step:6-



Repository class code:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mvc_with_Repository.Models;
using System.Data;

namespace mvc_with_Repository.Repository
{
    public class Repository_Employee
    {
        MVC_test_29_mayEntities _obj = new MVC_test_29_mayEntities();
        public void Emp_Insert(Employee _emp)
        {
            _obj.Employees.Add(_emp);
            _obj.SaveChanges();
        }

        public void Emp_Update(Employee _emp1)
        {
            _obj.Entry(_emp1).State = EntityState.Modified;
            _obj.SaveChanges();
        }

        public IEnumerable<Employee> Emp_Get()
        {
            var _dt = _obj.Employees.ToList();
            return _dt;
        }

        public void Emp_Delete(int A)
        {
            var _dt = _obj.Employees.Find(A);
            _obj.Employees.Remove(_dt);
            _obj.SaveChanges();
        }

        public Employee Emp_Edit(int A)
        {
            var _dt = _obj.Employees.Find(A);
            return _dt;
        }
      
    }
}

Step4:- add  the controller in a project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mvc_with_Repository.Models;
using mvc_with_Repository.Repository;

namespace mvc_with_Repository.Controllers
{
    public class EmpController : Controller
    {
        MVC_test_29_mayEntities _obj = new MVC_test_29_mayEntities();
        Repository_Employee _rep = new Repository_Employee();
        [HttpGet]
        public ActionResult Insert()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Insert(Employee _emp)
        {
            _rep.Emp_Insert(_emp);
            return RedirectToAction("Get");
        }
        public ActionResult Get()
        {
            var _dt = _rep.Emp_Get();
            return View(_dt);
        }

        public ActionResult Del(int A)
        {
            _rep.Emp_Delete(A);
            return RedirectToAction("Get");
        }

        public ActionResult Edt(int A)
        {
            var _dt = _rep.Emp_Edit(A);
            return View(_dt);
        }

        public ActionResult Update(Employee _emp)
        {
            _rep.Emp_Update(_emp);
            return RedirectToAction("Get");
        }
    }   
}

Step5:- Cretea Insert View (Insert.cshtml)

@model mvc_with_Repository.Models.Employee
@{
    ViewBag.Title = "Insert";
}

<h2>Insert</h2>
@using(@Html.BeginForm("Insert","Emp",FormMethod.Post))
{
    <table>
        <tr>
            <td>Name:</td>
            <td>@Html.TextBoxFor(x=>x.Name)</td>
        </tr>

        <tr>
            <td>Age:</td>
            <td>@Html.TextBoxFor(x=>x.Age)</td>
        </tr>

        <tr>
            <td>Address:</td>
            <td>@Html.TextBoxFor(x=>x.Address)</td>
        </tr>

        <tr>
            <td></td>
            <td><input type="submit" id="btnsave" value="Save" /></td>
        </tr>
    </table>
}


Step6:- Create Add Get View(Get.cshtml)

@model IEnumerable<mvc_with_Repository.Models.Employee>
@{
    ViewBag.Title = "Get";
}

<h2>Get</h2>
@using(@Html.BeginForm("Get","Emp",FormMethod.Get))
{
<table border="1" style="background-color:pink">
    <tr style="background-color:maroon;color:white">
        <th>Name</th>
        <th>Age</th>
        <th>Address</th>
        <th></th>
        <th></th>
    </tr>
    @foreach(var pp in Model)
    { 
    <tr>
        <td>@pp.Name</td>
        <td>@pp.Age</td>
        <td>@pp.Address</td>
        <td>@Html.ActionLink("Delete", "Del", new {A=@pp.Empid})</td>
        <td>@Html.ActionLink("Edit", "Edt", new {A=@pp.Empid})</td>
    </tr>
    }
</table>
}


Step7:- Add Edit View (Edt.cshtml)

@model mvc_with_Repository.Models.Employee
@{
    ViewBag.Title = "Edt";
}

<h2>Edt</h2>
@using(@Html.BeginForm("Update","Emp",FormMethod.Post))
{
   <table>
       <tr>
           @Html.HiddenFor(x=>x.Empid)
           <td>Name:</td>
           <td>@Html.TextBoxFor(x=>x.Name)</td>
       </tr>

       <tr>
           <td>Age:</td>
           <td>@Html.TextBoxFor(x=>x.Age)</td>
       </tr>

       <tr>
           <td>Address:</td>
           <td>@Html.TextBoxFor(x=>x.Address)</td>
       </tr>

       <tr>
           <td></td>
           <td><input type="submit" value="Update" /></td>
       </tr>
   </table>
}

No comments:
Write comments

Popular Posts

Popular Posts

Translate

Total Pageviews