MVC CRUD Operations Using Entity Framework
In this article we have learn about MVC CRUD Operations Using Entity Framework without writing any code. First, you should learn about MVC and the basics of Entity Framework.
MVC :
- The View is responsible for the look and feel.
- Model represents the real world object and provides data to the View.
- The Controller is responsible for taking the end user’s request and loading the appropriate Model and View.
Step 1 : Open visual studio Go to File then Select New then Select Project and Add new project
Step 2 :Now add Ado.net Entity Data model
Step 3 : Select data modal from the database as following
Step 4 : then submit finish
Step 5: Add Controller as below
Step 5 : when you add controller crud code can be added as following
public ActionResult Details(int id = 0)
{
tbl_user tbl_user = db.tbl_user.Find(id);
if (tbl_user == null)
{
return HttpNotFound();
}
return View(tbl_user);
}
//
// GET: /ABC/Create
public ActionResult Create()
{
return View();
}
//
// POST: /ABC/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(tbl_user tbl_user)
{
if (ModelState.IsValid)
{
db.tbl_user.Add(tbl_user);
db.SaveChanges();
return RedirectToAction(“Index”);
}
return View(tbl_user);
}
//
// GET: /ABC/Edit/5
public ActionResult Edit(int id = 0)
{
tbl_user tbl_user = db.tbl_user.Find(id);
if (tbl_user == null)
{
return HttpNotFound();
}
return View(tbl_user);
}
//
// POST: /ABC/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(tbl_user tbl_user)
{
if (ModelState.IsValid)
{
db.Entry(tbl_user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction(“Index”);
}
return View(tbl_user);
}
//
// GET: /ABC/Delete/5
public ActionResult Delete(int id = 0)
{
tbl_user tbl_user = db.tbl_user.Find(id);
if (tbl_user == null)
{
return HttpNotFound();
}
return View(tbl_user);
}
//
// POST: /ABC/Delete/5
[HttpPost, ActionName(“Delete”)]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
tbl_user tbl_user = db.tbl_user.Find(id);
db.tbl_user.Remove(tbl_user);
db.SaveChanges();
return RedirectToAction(“Index”);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
Step 6: After adding controller you can need to add view for crude operation.
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>tbl_user</legend>
<div class=”editor-label”>
@Html.LabelFor(model => model.Name)
</div>
<div class=”editor-field”>
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class=”editor-label”>
@Html.LabelFor(model => model.Address)
</div>
<div class=”editor-field”>
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<div class=”editor-label”>
@Html.LabelFor(model => model.Phone)
</div>
<div class=”editor-field”>
@Html.EditorFor(model => model.Phone)
@Html.ValidationMessageFor(model => model.Phone)
</div>
<p>
<input type=”submit” value=”Create” />
</p>
</fieldset>
}
Now run your mvc project and see output, if you need help regarding this article the feel free to comment on below comment section.
SEE MORE
- Auto Refresh Partial View in ASP.NET MVC
- What is ASP.NET Core
- Difference between TempData keep() And Peek() in Asp.Net MVC
- Difference between viewbag,viewdata and tempdata in asp.net mvc
- ASP.NET MVC With AngularJS
- Retrieving Data Using Form Collection and Inserting Into ASP.Net MVC
- MVC CRUD Operations Using Entity Framework
- Search Functionality in ASP.NET MVC
- How to create a User Registration page using asp.net mvc
- Store Multiple Checkbox state from cookie using Jquery
- Cascading Dropdownlist using Ajax in Asp.Net Mvc with city state country
- Insert, Update, Delete In GridView Using ASP.Net C#
- Binding Dropdownlist With Database In Asp.Net MVC
- Search and Filter data in Gridview using Asp.net MVC
- Select Insert, Update And Delete With ASP.NET MVC
- Display Data in GridView Using Asp.net MVC
- Validation in ASP.NET MVC Razor view
- CRUD Operation Using 3-Tier Architecture In ASP.NET
- How to get Connection String from Web.Config in Asp.Net C#
- Login page using 3-Tier Architecture in ASP.Net
- Asp.Net Image Upload in 3-Tier Architecture and store in sql database
10 Comments