MVC CRUD Operations Using Entity Framework

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

MVC CRUD Operations Using Entity Framework

Step 3 :  Select data modal from the database as following

MVC CRUD Operations Using Entity Framework

Step 4 : then submit finish

Entity crude operation

Step 5: Add Controller as below

MVC CRUD Operations Using Entity Framework

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

6 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *