Install NuGet EntityFramework
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Roll { get; set; }
}
public class StudentDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
}
public class StudentsController : Controller
{
private StudentDbContext _dbContext = new StudentDbContext();
// GET: Students
public async Task<ActionResult> Index()
{
return View(await _dbContext.Students.ToListAsync());
}
// GET: Students/Details/5
public async Task<ActionResult> Details(int? id)
{
if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Student student = await _dbContext.Students.FindAsync(id);
if (student == null) return HttpNotFound();
return View(student);
}
// GET: Students/Create
public ActionResult Create()
{
return View();
}
// POST: Students/Create
[HttpPost]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Roll")] Student student)
{
if (ModelState.IsValid)
{
_dbContext.Students.Add(student);
await _dbContext.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(student);
}
// GET: Students/Edit/5
public async Task<ActionResult> Edit(int? id)
{
if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Student student = await _dbContext.Students.FindAsync(id);
if (student == null) return HttpNotFound();
return View(student);
}
// POST: Students/Edit/5
[HttpPost]
public async Task<ActionResult> Edit([Bind(Include = "Id,Name,Roll")] Student student)
{
if (ModelState.IsValid)
{
_dbContext.Entry(student).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(student);
}
public async Task<ActionResult> Delete(int? id)
{
if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Student student = await _dbContext.Students.FindAsync(id);
if (student == null) return HttpNotFound();
return View(student);
}
// POST: Students/Delete/5
[HttpPost, ActionName("Delete")]
public async Task<ActionResult> DeleteConfirmed(int id)
{
Student student = await _dbContext.Students.FindAsync(id);
_dbContext.Students.Remove(student);
await _dbContext.SaveChangesAsync();
return RedirectToAction("Index");
}
}
Index.cshtml
@model IEnumerable<CrudScaffolding.Models.Student>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Roll)
</th>
<th>
Actions
</th>
</tr>
</thead>
<tbody>
@foreach (var studentObj in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => studentObj.Name)
</td>
<td>
@Html.DisplayFor(modelItem => studentObj.Roll)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = studentObj.Id })
@Html.ActionLink("Details", "Details", new { id = studentObj.Id })
@Html.ActionLink("Delete", "Delete", new { id = studentObj.Id })
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model CrudScaffolding.Models.Student
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<br />
<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 { HtmlTextWriterAttribute = new { @class = "control-label" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Roll, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Roll, new { HtmlTextWriterAttribute = new { @class = "control-label" } })
</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");
}
Delete.cshtml
@model CrudScaffolding.Models.Student
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Confirm Delete</h3>
<div>
<h4>Student</h4>
<br />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dt>
@Html.DisplayFor(model => model.Name)
</dt>
<dt>
@Html.DisplayNameFor(model => model.Roll)
</dt>
<dt>
@Html.DisplayFor(model => model.Roll)
</dt>
</dl>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-danger" />
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>
Details.cshtml
@model CrudScaffolding.Models.Student
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Student</h4>
<br />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dt>
@Html.DisplayFor(model => model.Name)
</dt>
<dt>
@Html.DisplayNameFor(model => model.Roll)
</dt>
<dt>
@Html.DisplayFor(model => model.Roll)
</dt>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Id })
@Html.ActionLink("Back to List", "Index")
</p>
Edit.cshtml
@model CrudScaffolding.Models.Student
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<br />
@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 { HtmlTextWriterAttribute = new { @class = "control-label" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Roll, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Roll, new { HtmlTextWriterAttribute = new { @class = "control-label" } })
</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");
}
0 Comments