In the previous tutorial we created an MVC application that displays data using the Entity Framework and SQL Server Database. In this tutorial we will customize the Create, Update and Delete (CRUD) code using Entity Framework.
Also, we add a <p> tag above the <table> tag and provide the following code:
- public ActionResult Create()
- {
- return View();
- }
- @model EFRndProject.Models.Customer
- @{
- ViewBag.Title = "Create";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>Create</h2>
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
- <div class="form-horizontal">
- <h4>Customer</h4>
- <hr />
- @Html.ValidationSummary(true)
- <div class="form-group">
- @Html.LabelFor(model => model.CustomerName, new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.CustomerName)
- @Html.ValidationMessageFor(model => model.CustomerName)
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.CustAddress, new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.CustAddress)
- @Html.ValidationMessageFor(model => model.CustAddress)
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.CustCity, new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.CustCity)
- @Html.ValidationMessageFor(model => model.CustCity)
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.CustPinCode, new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.CustPinCode)
- @Html.ValidationMessageFor(model => model.CustPinCode)
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.PhoneNo, new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.PhoneNo)
- @Html.ValidationMessageFor(model => model.PhoneNo)
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Create" class="btn btn-default" />
- </div>
- </div>
- </div>
- }
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Create([Bind(Include="CustomerID,CustomerName,CustAddress,CustCity,CustPinCode,PhoneNo")] Customer customer)
- {
- if (ModelState.IsValid)
- {
- db.Customers.Add(customer);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(customer);
- }
- public ActionResult Edit(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Customer customer = db.Customers.Find(id);
- if (customer == null)
- {
- return HttpNotFound();
- }
- return View(customer);
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit([Bind(Include="CustomerID,CustomerName,CustAddress,CustCity,CustPinCode,PhoneNo")] Customer customer)
- {
- if (ModelState.IsValid)
- {
- db.Entry(customer).State = EntityState.Modified;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(customer);
- }
- @model EFRndProject.Models.Customer
- @{
- ViewBag.Title = "Edit";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>Edit</h2>
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
- <div class="form-horizontal">
- <h4>Customer</h4>
- <hr />
- @Html.ValidationSummary(true)
- @Html.HiddenFor(model => model.CustomerID)
- <div class="form-group">
- @Html.LabelFor(model => model.CustomerName, new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.CustomerName)
- @Html.ValidationMessageFor(model => model.CustomerName)
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.CustAddress, new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.CustAddress)
- @Html.ValidationMessageFor(model => model.CustAddress)
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.CustCity, new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.CustCity)
- @Html.ValidationMessageFor(model => model.CustCity)
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.CustPinCode, new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.CustPinCode)
- @Html.ValidationMessageFor(model => model.CustPinCode)
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.PhoneNo, new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.PhoneNo)
- @Html.ValidationMessageFor(model => model.PhoneNo)
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Save" class="btn btn-default" />
- </div>
- </div>
- </div>
- }
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
As you saw for the update and create operations, delete operations require two action methods. The method that is called in response to a GET request displays a view that gives the user a chance to approve or cancel the delete operation. If the user approves it, a POST request is created. When that happens, the HttpPost Delete method is called and then that method actually does the delete operation.
- public ActionResult Delete(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Customer customer = db.Customers.Find(id);
- if (customer == null)
- {
- return HttpNotFound();
- }
- return View(customer);
- }
- [HttpPost, ActionName("Delete")]
- [ValidateAntiForgeryToken]
- public ActionResult DeleteConfirmed(int id)
- {
- Customer customer = db.Customers.Find(id);
- db.Customers.Remove(customer);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- @model EFRndProject.Models.Customer
- @{
- ViewBag.Title = "Delete";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>Delete</h2>
- <h3>Are you sure you want to delete this?</h3>
- <div>
- <h4>Customer</h4>
- <hr />
- <dl class="dl-horizontal">
- <dt>
- @Html.DisplayNameFor(model => model.CustomerName)
- </dt>
- <dd>
- @Html.DisplayFor(model => model.CustomerName)
- </dd>
- <dt>
- @Html.DisplayNameFor(model => model.CustAddress)
- </dt>
- <dd>
- @Html.DisplayFor(model => model.CustAddress)
- </dd>
- <dt>
- @Html.DisplayNameFor(model => model.CustCity)
- </dt>
- <dd>
- @Html.DisplayFor(model => model.CustCity)
- </dd>
- <dt>
- @Html.DisplayNameFor(model => model.CustPinCode)
- </dt>
- <dd>
- @Html.DisplayFor(model => model.CustPinCode)
- </dd>
- <dt>
- @Html.DisplayNameFor(model => model.PhoneNo)
- </dt>
- <dd>
- @Html.DisplayFor(model => model.PhoneNo)
- </dd>
- </dl>
- @using (Html.BeginForm()) {
- @Html.AntiForgeryToken()
- <div class="form-actions no-color">
- <input type="submit" value="Delete" class="btn btn-default" /> |
- @Html.ActionLink("Back to List", "Index")
- </div>
- }
- </div>
- {
- db.Dispose();
- base.Dispose(disposing);
- }

Sr KarthigaPosted Apr 16, 2016, 4:34 AM
good one
Sr KarthigaPosted Apr 16, 2016, 4:34 AM
Nice explanation
Vithal WadjePosted Jan 3, 2015, 3:30 AM
nice explanation