This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Me And My Respected Teacher Mr Kamal Sheel Mishra

Mr. K.S. Mishra is HOD of Computer Science from SMS Varanasi where I have completed my MCA

Me And My Respected Teacher Mr Udayan Maiti

Mr. Udayan Maiti is a senior .Net Expert and has guided many professionals of multi national Companies(MNC)

Me And My Best Friend Mr Ravinder Goel

Mr. Ravinder Goel is a senior Software Engineer and now he is working Wipro Technology

Tuesday, 19 May 2015

Insert, Update, Delete Operation (CRUD Functionality) in Asp.Net MVC Using Entity Framework C#.Net

 Insert, Update, Delete Operation (CRUD Functionality) in Asp.Net MVC Using Entity Framework C#.Net ,

Note : In this concept we will discuss object as model until now we  have being using entity framework and entities ,Entities are mapped to database table and objects relationship mapping tool like entity Framework,Hibernate etc used to retrieve and save data.

create database test
use test

-- create table student

create table Student
(
RollNo int primary key,
Name varchar(50),
Gender varchar(50),
Course varchar(50),
Dob Date
)

-- create table CourseInfo

create table CourseInfo
(
CourseId int primary key,
CourseName varchar(50)
)


How to add entity Model in Mvc Application is shown below in the given figure………………………

Right click on Model Folder and new Entity Models the steps in form of pictures are given below.


 Next Step


Next Step



Next step we will add Controller  which  name is  HomeController  and  the  code  of  controller  is  given below

 Source Code of HomeController.cs…………………………….

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
// using this namespace
using System.Web.Mvc;
using CrudFunctionalityInMvc.Models;

namespace CrudFunctionalityInMvc.Controllers
{
public class HomeController : Controller
{
//create  instance of  entity class
testEntities tt = new testEntities();
public HomeController()
{

}

// this code for Index  View...................

public ActionResult Index()
{
//var v = from m in tt.Students select m;
return View(tt.Students.ToList());
}

// this code for Create View...................

public ActionResult Create()
{
ViewBag.Course = new SelectList(tt.CourseInfoes, "CourseId", "CourseName");
return View();
}

[HttpPost]
public ActionResult Create(Student ss)
{
tt.Students.Add(ss);
tt.SaveChanges();
return  RedirectToAction("Index");

}
// this code for  Delete  View...................

public ActionResult Delete(string RollNo)
{
int rn = Convert.ToInt32(RollNo);
var st = tt.Students.Where(m => m.RollNo ==rn).Select(m => m).FirstOrDefault();
return View(st);
}

[HttpPost]
public ActionResult Delete(string RollNo, bool? aa)
{
int rn = Convert.ToInt32(RollNo);
var st = tt.Students.Where(m => m.RollNo == rn).Select(m => m).FirstOrDefault();
tt.Students.Remove(st);
tt.SaveChanges();
return RedirectToAction("Index");

}

// this code for  Details  View...................

public ActionResult Details(string RollNo)
{
int rn = Convert.ToInt32(RollNo);
var st = tt.Students.Where(m => m.RollNo == rn).Select(m => m).FirstOrDefault();
return View(st);
}

// this code for Edit View...................

public ActionResult Edit(string RollNo)
{
int rn = Convert.ToInt32(RollNo);
var st = tt.Students.Where(m => m.RollNo == rn).Select(m => m).FirstOrDefault();
return View(st);
}
[HttpPost]
public ActionResult Edit(Student ss)
{
int rn = Convert.ToInt32(ss.RollNo);
var st = tt.Students.Where(m => m.RollNo == rn).Select(m => m).FirstOrDefault();
if (st != null)
{
st.Name = ss.Name;
st.Gender = ss.Gender;
st.Course = ss.Course;
st.Dob = ss.Dob;
tt.SaveChanges();
}
return RedirectToAction("Index");
}

}
}

How to add Index View using  ActionResult Index() which  is  given in HomeController the steps in form of pictures are given below.



Source Code of Index.cshtml…………………………….

@model IEnumerable<CrudFunctionalityInMvc.Models.Student>

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table border="1">
<tr>
<th>
@Html.DisplayNameFor(model => model.RollNo)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.Course)
</th>
<th>
@Html.DisplayNameFor(model => model.Dob)
</th>
<th>Action</th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.RollNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.Course)
</td>
<td>
@Html.DisplayFor(modelItem => item.Dob)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { RollNo=item.RollNo }) |
@Html.ActionLink("Details", "Details", new { RollNo=item.RollNo }) |
@Html.ActionLink("Delete", "Delete", new { RollNo=item.RollNo })
</td>
</tr>
}
</table>
</body>
</html>



How to add Create View using  ActionResult Create() which  is  given in HomeController the steps in form of pictures are given below.



Source Code of Create.cshtml…………………………….

@model CrudFunctionalityInMvc.Models.Student

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/ecmascript" >

$(document).ready(function ()
{
$('#ddlCourse').change(function (e)
{
var v = $("#ddlCourse option:selected").text();
$('#Course').val(v);
e.preventDefault();
});

});
</script>
</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>Student</legend>

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

<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.Gender)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Gender)
@Html.ValidationMessageFor(model => model.Gender)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Course)
</div>
<div class="editor-field">
@*  @Html.EditorFor(model => model.Course)
@Html.ValidationMessageFor(model => model.Course)*@
@*@Html.DropDownList("Course","Select")*@

@Html.DropDownList("ddlCourse", new SelectList(ViewBag.Course,"Value","Text"), "--Select Course--")
@Html.Hidden("Course")
</div>

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

<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>

How to add Delete View using  ActionResult Delete () which  is  given in HomeController the steps in form of pictures are given below.



Source Code of Delete.cshtml…………………………….

@model CrudFunctionalityInMvc.Models.Student

@{
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>Student</legend>

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

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

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

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

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

 How to add Details View using  ActionResult Details() which  is  given in HomeController the steps in form of pictures are given below.




 Source Code of Details.cshtml…………………………….

@model CrudFunctionalityInMvc.Models.Student

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Details</title>
</head>
<body>
<fieldset>
<legend>Student</legend>

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

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

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

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

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

 How to add Edit View using  ActionResult Edit() which  is  given in HomeController the steps in form of pictures are given below.

 Source Code of Edit.cshtml…………………………….

@model CrudFunctionalityInMvc.Models.Student

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
<legend>Student</legend>

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

<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.Gender)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Gender)
@Html.ValidationMessageFor(model => model.Gender)
</div>

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

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

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

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


Result