crud operation in mvc with db first approach




This is the example of basic Crud Operation in Asp.NET MVC with Database First Approach/DbFirst Approach of EntityFramework.In next blog i will show you how to use Stored Procedure in DbFirst Approach.In this example i am using visual Studio 2015 and Sql-Server 2014 on Windows7 operating System.So Lets Start:-



First Step:-Create a Project with name DbFirstDemo in your Visual Studio.

Second Step:-Open your Sql-Server and Create a table named with Student.
Third Step:-Click on Model Folder then Click on Add then Click on New Item now below window will show then click on Add button at bottom right.

Fourth Step:- Select Ef Designer from database and click on next and from next screen select your database.follow the screen shots one by one.. 

After Click on Finish on Last Screen above you will see this window

Fouth Step:-Click On Controller Folder ,Add a Controller with views using Entity Framework, now you will see below screen Add Student in Model class and Select DbFirstProjectEntities in DbContext Option ,other option leave as it is.

Now Your Project will be ready with Controller Class and View. Run it enter data.
Controller Class in Controller Folder:-

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.Entity;

using System.Linq;

using System.Net;

using System.Web;

using System.Web.Mvc;

using DbFirstDemo.Models;

 

namespace DbFirstDemo.Controllers

{

    public class StudentController : Controller

    {

        private DbFirstProjectEntities db = new DbFirstProjectEntities();

 

        // GET: Student

        public ActionResult Index()

        {

            return View(db.Students.ToList());

        }

 

        // GET: Student/Details/5

        public ActionResult Details(int? id)

        {

            if (id == null)

            {

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            }

            Student student = db.Students.Find(id);

            if (student == null)

            {

                return HttpNotFound();

            }

            return View(student);

        }

 

        // GET: Student/Create

        public ActionResult Create()

        {

            return View();

        }

 

        // POST: Student/Create

        // To protect from overposting attacks, please enable the specific properties you want to bind to, for

        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.

        [HttpPost]

        [ValidateAntiForgeryToken]

        public ActionResult Create([Bind(Include = "Id,Name,Age,City")] Student student)

        {

            if (ModelState.IsValid)

            {

                db.Students.Add(student);

                db.SaveChanges();

                return RedirectToAction("Index");

            }

 

            return View(student);

        }

 

        // GET: Student/Edit/5

        public ActionResult Edit(int? id)

        {

            if (id == null)

            {

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            }

            Student student = db.Students.Find(id);

            if (student == null)

            {

                return HttpNotFound();

            }

            return View(student);

        }

 

        // POST: Student/Edit/5

        // To protect from overposting attacks, please enable the specific properties you want to bind to, for

        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.

        [HttpPost]

        [ValidateAntiForgeryToken]

        public ActionResult Edit([Bind(Include = "Id,Name,Age,City")] Student student)

        {

            if (ModelState.IsValid)

            {

                db.Entry(student).State = EntityState.Modified;

                db.SaveChanges();

                return RedirectToAction("Index");

            }

            return View(student);

        }

 

        // GET: Student/Delete/5

        public ActionResult Delete(int? id)

        {

            if (id == null)

            {

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            }

            Student student = db.Students.Find(id);

            if (student == null)

            {

                return HttpNotFound();

            }

            return View(student);

        }

 

        // POST: Student/Delete/5

        [HttpPost, ActionName("Delete")]

        [ValidateAntiForgeryToken]

        public ActionResult DeleteConfirmed(int id)

        {

            Student student = db.Students.Find(id);

            db.Students.Remove(student);

            db.SaveChanges();

            return RedirectToAction("Index");

        }

 

        protected override void Dispose(bool disposing)

        {

            if (disposing)

            {

                db.Dispose();

            }

            base.Dispose(disposing);

        }

    }

}


Create,Edit,Delete,Details Views in Views Folder.

1.Create View

@model DbFirstDemo.Models.Student

 

@{

    ViewBag.Title = "Create";

}

 

<h2>Create</h2>

 @using (Html.BeginForm())

{

    @Html.AntiForgeryToken()

   

    <div class="form-horizontal">

        <h4>Student</h4>

        <hr />

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">

            @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })

            </div>

        </div>

 

        <div class="form-group">

            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })

            </div>

        </div>

 

        <div class="form-group">

            @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })

            </div>

        </div>

 

        <div class="form-group">

            @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })

            </div>

        </div>

 

        <div class="form-group">

            <div class="col-md-offset-2 col-md-10">

                <input type="submit" value="Create" class="btn btn-success"/>

            </div>

        </div>

    </div>

}

 

<div>

    @Html.ActionLink("Back to List", "Index")

</div> 

@section Scripts {

    @Scripts.Render("~/bundles/jqueryval")

}


2.Delete View

@model DbFirstDemo.Models.Student

 

@{

    ViewBag.Title = "Delete";

}

 

<h2>Delete</h2>

 

<h3>Are you sure you want to delete this?</h3>

<div>

    <h4>Student</h4>

    <hr />

    <dl class="dl-horizontal">

        <dt>

            @Html.DisplayNameFor(model => model.Name)

        </dt>

 

        <dd>

            @Html.DisplayFor(model => model.Name)

        </dd>

 

        <dt>

            @Html.DisplayNameFor(model => model.Age)

        </dt>

 

        <dd>

            @Html.DisplayFor(model => model.Age)

        </dd>

 

        <dt>

            @Html.DisplayNameFor(model => model.City)

        </dt>

 

        <dd>

            @Html.DisplayFor(model => model.City)

        </dd>

 

    </dl>

 

    @using (Html.BeginForm()) {

        @Html.AntiForgeryToken()

 

        <div class="form-actions no-color">

            <input type="submit" value="Delete" class="btn btn-default" /> |

            @Html.ActionLink("Back to List", "Index")

        </div>

    }

</div>

 

3.Details View

@model DbFirstDemo.Models.Student

 

@{

    ViewBag.Title = "Details";

}

 

<h2>Details</h2>

 

<div>

    <h4>Student</h4>

    <hr />

    <dl class="dl-horizontal">

        <dt>

            @Html.DisplayNameFor(model => model.Name)

        </dt>

 

        <dd>

            @Html.DisplayFor(model => model.Name)

        </dd>

 

        <dt>

            @Html.DisplayNameFor(model => model.Age)

        </dt>

 

        <dd>

            @Html.DisplayFor(model => model.Age)

        </dd>

 

        <dt>

            @Html.DisplayNameFor(model => model.City)

        </dt>

 

        <dd>

            @Html.DisplayFor(model => model.City)

        </dd>

 

    </dl>

</div>

<p>

    @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |

    @Html.ActionLink("Back to List", "Index")

</p>

 4.Edit View

@model DbFirstDemo.Models.Student

 

@{

    ViewBag.Title = "Edit";

}

 

<h2>Edit</h2>

 

 

@using (Html.BeginForm())

{

    @Html.AntiForgeryToken()

   

    <div class="form-horizontal">

        <h4>Student</h4>

        <hr />

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @Html.HiddenFor(model => model.Id)

 

        <div class="form-group">

            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })

            </div>

        </div>

 

        <div class="form-group">

            @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })

            </div>

        </div>

 

        <div class="form-group">

            @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })

            </div>

        </div>

 

        <div class="form-group">

            <div class="col-md-offset-2 col-md-10">

                <input type="submit" value="Save" class="btn btn-default" />

            </div>

        </div>

    </div>

}

 

<div>

    @Html.ActionLink("Back to List", "Index")

</div>

 

@section Scripts {

    @Scripts.Render("~/bundles/jqueryval")

}

 

5.Index View

@model IEnumerable<DbFirstDemo.Models.Student>

 

@{

    ViewBag.Title = "Index";

}

 

<h2>Index</h2>

 

<p>

    @Html.ActionLink("Create New", "Create")

</p>

<table class="table">

    <tr>

        <th>

            @Html.DisplayNameFor(model => model.Name)

        </th>

        <th>

            @Html.DisplayNameFor(model => model.Age)

        </th>

        <th>

            @Html.DisplayNameFor(model => model.City)

        </th>

        <th></th>

    </tr>

 

@foreach (var item in Model) {

    <tr>

        <td>

            @Html.DisplayFor(modelItem => item.Name)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.Age)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.City)

        </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>

   End..


No comments:

Post a Comment