---
title: "CRUD operation in MVC with Repository Pattern"  
description: "Insert, Update and Delete operation in MVC with the Help of Repository Pattern."  
author: "Anonymous User"  
published: 2018-09-05  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/13108/crud-operation-in-mvc-with-repository-pattern  
category: "c#"  
tags: ["mvc", "mvc4"]  
reading_time: 8 minutes  

---

# CRUD operation in MVC with Repository Pattern

**[Repository Pattern](https://www.mindstick.com/articles/1565/crud-operations-using-generic-repository-pattern-in-asp-dot-net-mvc-4) in MVC**

1). Create A table “BookTbl” in the database.

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/c001e8bc-0e89-4331-8bdd-02a314ba7765.png)

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/2c7e1473-86ef-45c4-87ce-13faac59d2fa.png)

2). Open [Visual Studio](https://www.mindstick.com/forum/12863/how-to-insert-a-video-in-asp-dot-net-web-page-in-visual-studio-2010) and create a new [project](https://www.mindstick.com/articles/105927/how-to-excel-at-managing-multiple-projects) any name like “RepositoryFst”

And add a new folder into the Model Folder like “DAL”. Here DAL means [Data Access](https://www.mindstick.com/blog/301551/risks-and-challenges-of-data-access-and-sharing) Layer and adds to different [class file](https://www.mindstick.com/forum/60/how-we-add-dll-with-other-class-file) in this folder first add an [interface](https://www.mindstick.com/blog/676/interface) file and [second](https://answers.mindstick.com/qa/39871/where-was-the-second-continental-congress-held-this-was-the-meeting-during-which-the-declaration-of-independence-was-ratified) a normal class file like this.

RightClick on ModelFolder -> ADD -> New Folder -> DAL(name).

Select DALfolder -> RightClick -> Add -> New item -> Interface -> IBookRepository.cs(name).

Select DALfolder -> RightClick -> Add -> New item -> Class -> BookRepository.cs(name).

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/34042fa1-697f-4e99-b9a3-3936ab0c60a0.png)

3). Select Model Folder and RightClick on it. Then fallow it-

Select ModelFolder -> Add -> NewItem -> Data -> ADO.Net Entity Data Model

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/dff14962-e06c-4a70-ace7-95ce00dff96d.png)

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/e48f8f80-0f65-4094-8f76-833eb5c7457b.png)

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/6f129204-9856-407b-a31a-c2e45d4a3734.png)

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/969ab9c0-e110-486d-ba8e-292ef918c787.png)

4). IBookRepository.cs

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryFst.Models.DAL
{
    interface IBookRepository
    {
        IEnumerable<BookTbl> GetBooks();
        BookTbl GetBookByID(int bookId);
        void InsertBook(BookTbl book);
        void DeleteBook(int bookId);
        void UpdateBook(BookTbl book);
        void Save();
    }
}
```

5). BookRepository.cs

```
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace RepositoryFst.Models.DAL
{
    public class BookRepository : IBookRepository
    {
        private DataContext _context;

        public BookRepository(DataContext context)
        {
            _context = context;
        }

        public IEnumerable<BookTbl> GetBooks()
        {
            return _context.BookTbls.ToList();
        }

        public BookTbl GetBookByID(int bookId)
        {
            return _context.BookTbls.Find(bookId);
        }

        public void InsertBook(BookTbl book)
        {
            _context.BookTbls.Add(book);
        }

        public void DeleteBook(int bookId)
        {
            BookTbl book = _context.BookTbls.Find(bookId);
            _context.BookTbls.Remove(book);
        }

        public void UpdateBook(BookTbl book)
        {
            _context.Entry(book).State = EntityState.Modified;
        }

        public void Save()
        {
            _context.SaveChanges();
        }
    }
}
```

6). HomeController.cs

(add this file in controller folder)

```
using RepositoryFst.Models;using RepositoryFst.Models.DAL;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;
namespace RepositoryFst.Controllers{    public class HomeController : Controller    {        private IBookRepository interfaceobj;
        public HomeController()        {            interfaceobj = new BookRepository(new DataContext());        }
        public ActionResult Index()        {            var data = from m in interfaceobj.GetBooks() select m;            return View(data);        }
        public ActionResult Create()        {            return View();        }
        [HttpPost]        public ActionResult Create(BookTbl book)        {            interfaceobj.InsertBook(book);            interfaceobj.Save();            return RedirectToAction("Index");        }
        public ActionResult Details(int id)        {            BookTbl b = interfaceobj.GetBookByID(id);            return View(b);        }
        public ActionResult Delete(int id)        {            interfaceobj.DeleteBook(id);            interfaceobj.Save();            return RedirectToAction("Index");        }
        public ActionResult Edit(int id)        {            BookTbl B = interfaceobj.GetBookByID(id);            return View(B);        }
        [HttpPost]        public ActionResult Edit(int id, BookTbl book)        {            interfaceobj.UpdateBook(book);            interfaceobj.Save();            return RedirectToAction("Index");        }    }}
```

7). Add Index View for display all data from that table.

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/4be8892e-8616-4a2f-8158-033a9d6ab128.png)\

```
@model IEnumerable<RepositoryFst.Models.BookTbl>@{    ViewBag.Title = "Index";}<h2>Index</h2><p>    @Html.ActionLink("Create New", "Create")</p>
<table>    <tr>        <th>            @Html.DisplayNameFor(model => model.BookName)        </th>
        <th>            @Html.DisplayNameFor(model => model.BookAuthor)        </th>
        <th></th>    </tr>
@foreach (var item in Model) {    <tr>        <td>            @Html.DisplayFor(modelItem => item.BookName)        </td>        <td>            @Html.DisplayFor(modelItem => item.BookAuthor)        </td>        <td>            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>}
</table>
```

8). Details View

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/6c10246e-5001-4fe1-bc9b-5372f708bab4.png)\

```
@model RepositoryFst.Models.BookTbl@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Details</title></head>
<body>    <fieldset>        <legend>BookTbl</legend>
        <div class="display-label">             @Html.DisplayNameFor(model => model.BookName)        </div>
        <div class="display-field">            @Html.DisplayFor(model => model.BookName)        </div>
        <div class="display-label">
             @Html.DisplayNameFor(model => model.BookAuthor)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.BookAuthor)
        </div>
    </fieldset>
    <p>        @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
        @Html.ActionLink("Back to List", "Index")
    </p>
</body>
</html>
```

9). Add View(For Insert record)

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/bc3889a4-097f-4124-b289-72f6d20792d6.png)\

```
@model RepositoryFst.Models.BookTbl@{    ViewBag.Title = "Create";}
<h2>Create</h2>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>BookTbl</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.BookName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BookName)
            @Html.ValidationMessageFor(model => model.BookName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.BookAuthor)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BookAuthor)
            @Html.ValidationMessageFor(model => model.BookAuthor)
        </div>
        <p>            <input type="submit" value="Create" />        </p>
    </fieldset>}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
```

10). Delete Record view

![CRUD operation in MVC with Repository Pattern](https://www.mindstick.com/mindstickarticle/72066bbf-936a-41a3-b358-583fd6f69bf0/images/13290627-446c-455e-a816-523df6ccaf20.png)\

```
@model RepositoryFst.Models.BookTbl@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Delete</title></head>
<body>
    <h3>Are you sure you want to delete this?</h3>
    <fieldset>
        <legend>BookTbl</legend>
        <div class="display-label">             @Html.DisplayNameFor(model => model.BookName)        </div>        <div class="display-field">            @Html.DisplayFor(model => model.BookName)        </div>
        <div class="display-label">             @Html.DisplayNameFor(model => model.BookAuthor)        </div>        <div class="display-field">            @Html.DisplayFor(model => model.BookAuthor)        </div>    </fieldset>    @using (Html.BeginForm()) {        <p>            <input type="submit" value="Delete" /> |            @Html.ActionLink("Back to List", "Index")        </p>    }</body></html>
```

11). Edit/Update Record view

```
@model RepositoryFst.Models.BookTbl
@{
    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Edit</title></head><body>    <script src="~/Scripts/jquery-1.7.1.min.js"></script>    <script src="~/Scripts/jquery.validate.min.js"></script>    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    @using (Html.BeginForm()) {        @Html.ValidationSummary(true)
        <fieldset>            <legend>BookTbl</legend>
            @Html.HiddenFor(model => model.ID)
            <div class="editor-label">                @Html.LabelFor(model => model.BookName)            </div>
            <div class="editor-field">                @Html.EditorFor(model => model.BookName)                @Html.ValidationMessageFor(model => model.BookName)            </div>            <div class="editor-label">                @Html.LabelFor(model => model.BookAuthor)            </div>            <div class="editor-field">                @Html.EditorFor(model => model.BookAuthor)                @Html.ValidationMessageFor(model => model.BookAuthor)            </div>            <p>                <input type="submit" value="Save" />            </p>        </fieldset>    }    <div>        @Html.ActionLink("Back to List", "Index")    </div></body></html>
```

\

---

Original Source: https://www.mindstick.com/articles/13108/crud-operation-in-mvc-with-repository-pattern

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
