How to use Crud Operations in MVC4 with Linq Concept


In this Article, I am going to explain about how to use CRUD Operations in MVC4 with LINQ to SQL. I have implemented CRUD Operations (Create,Retrieve,Update,Delete) and explain server side code for MVC4 Linq to SQL . I have post source which is given below.

What is LINQ?


LINQ stands Language Integrated Query


  1. LINQ will be faster than SQL

  2. It will retrieve the data by using the LINQ object

  3. Object based data fetching

  4. Retrieve records Quickly

  5. We can easily convert list and Ilist object




How to List the Records from Database using LINQ to SQL



In the Index method by using the LINQ object it is fetching the data.


public ActionResult Index()
{
using (DataClasses1DataContext context = new DataClasses1DataContext())
{

var Emplists = from Emplist in context.TblEmps
orderby Emplist.EmpName
select Emplist;

return View(Emplists.ToList());
}
}

How to Add Records using Database with LINQ



There are two types of methods HttpPost and HttpGet Post method is used to save the record by inserting the values. Get method is used to show the entry form design

[HttpPost]
public ActionResult Create(FormCollection frm1)
{
using (DataClasses1DataContext context = new DataClasses1DataContext())
{
TblEmp tbl = new TblEmp();
tbl.EmpName = frm1["EmpNo"];
tbl.EmpName = frm1["EmpName"];
context.TblEmps.InsertOnSubmit(tbl);
context.SubmitChanges();
}
return View();
}

[HttpGet]
public ActionResult Create()
{
return View();
}


How to Edit Records using Database with LINQ


There are two types of methods HttpPost and HttpGet Post method is used to edit the record which is inserted.Get method is used to show the entry form design while the record is selected

[HttpPost]
public ActionResult Edit(FormCollection frm1)
{
using (DataClasses1DataContext context = new DataClasses1DataContext())
{
var model = (from t in context.TblEmps where t.Id == Convert.ToInt32(frm1["id"]) select t).Single();
TblEmp tbl = new TblEmp();
tbl.EmpNo = frm1["EmpNo"];
tbl.EmpName = frm1["EmpName"];
context.TblEmps.InsertOnSubmit(tbl);
context.SubmitChanges();
}
return View();
}

[HttpGet]
public ActionResult Edit(int id)
{
DataClasses1DataContext context = new DataClasses1DataContext();
var model = (from t in context.TblEmps where t.Id == id select t).ToList();
return View(model[0]);
}

How to Delete Records using Database with LINQ


There are two types of methods HttpPost and HttpGet Post method is used to delete the record.
Get method is used to show the entry form design while the record is selected


[HttpPost]
public ActionResult Delete(FormCollection frm1)
{
DataClasses1DataContext context = new DataClasses1DataContext();
var DeleteID = Convert.ToInt32(Session["Id"]); //Convert.ToInt32(frm1["id"]);
TblEmp toDelete = db.TblEmps.Single(p => p.Id == DeleteID);
context.TblEmps.DeleteOnSubmit(toDelete);
context.SubmitChanges();
return View();
}

[HttpGet]
public ActionResult Delete(int id)
{
Session["Id"] = id;
DataClasses1DataContext context = new DataClasses1DataContext();
var model = (from t in context.TblEmps where t.Id == id select t).ToList();
return View(model[0]);
}

How to Show Details View using Database with Linq



In the Details method calling from our Controller for Details the records for Post Method

[HttpPost]
public ActionResult Details()
{
return View();
}


How to Details Records using Database with Linq


In the Details method calling from our Controller for Details the records for Get Method

[HttpGet]
public ActionResult Details(int id)
{
DataClasses1DataContext q1 = new DataClasses1DataContext();
var model = (from t in q1.TblEmps where t.Id == id select t).ToList();
return View(model[0]);
}


Razor View


This is View for Create The Records


Create View

@model MvcApplication1.Controllers.TblEmp

@{
ViewBag.Title = "Create";
}

Create



@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
<legend>TblEmp</legend>

<div class="editor-label">
@Html.LabelFor(model => model.EmpNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmpNo)
@Html.ValidationMessageFor(model => model.EmpNo)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.EmpName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmpName)
@Html.ValidationMessageFor(model => model.EmpName)
</div>

<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

List View
@model IEnumerable<MvcApplication1.Controllers.TblEmp>

@{
ViewBag.Title = "Index";
}

Index



<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.EmpNo)
</th>
<th>
@Html.DisplayNameFor(model => model.EmpName)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmpNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmpName)
</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>

This is View for Edit The Records

Edit View
@model MvcApplication1.Controllers.TblEmp

@{
ViewBag.Title = "Edit";
}

Edit



@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
<legend>TblEmp</legend>

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

<div class="editor-label">
@Html.LabelFor(model => model.EmpNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmpNo)
@Html.ValidationMessageFor(model => model.EmpNo)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.EmpName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmpName)
@Html.ValidationMessageFor(model => model.EmpName)
</div>

<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}


This is View for Delete The Records

Delete View
@model MvcApplication1.Controllers.TblEmp

@{
ViewBag.Title = "Delete";
}

Delete



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

<div class="display-label">
@Html.DisplayNameFor(model => model.Id)
@Html.DisplayNameFor(model => model.EmpNo)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.EmpNo)
</div>

<div class="display-label">
@Html.DisplayNameFor(model => model.EmpName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.EmpName)
</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}

This is View for Details The Records

Details View
@model MvcApplication1.Controllers.TblEmp

@{
ViewBag.Title = "Details";
}

Details



<fieldset>
<legend>TblEmp</legend>

<div class="display-label">
@Html.DisplayNameFor(model => model.EmpNo)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.EmpNo)
</div>

<div class="display-label">
@Html.DisplayNameFor(model => model.EmpName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.EmpName)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.Id }) |
@Html.ActionLink("Back to List", "Index")
</p>


Attachments

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: