{"id":9167,"date":"2021-03-25T17:13:51","date_gmt":"2021-03-25T11:43:51","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=9167"},"modified":"2024-06-06T19:39:34","modified_gmt":"2024-06-06T14:09:34","slug":"asp-net-mvc-scaffolding","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/asp-net-mvc-scaffolding\/","title":{"rendered":"ASP.NET MVC Scaffolding"},"content":{"rendered":"\n<p>MVC Scaffolding is a feature of ASP.NET that allows you to generate functional code rapidly. It is also known as a code generator framework and is pre-installed in Visual Studio 2013 and a higher version.<\/p>\n\n\n\n<p>To create a basic CRUD application, scaffolding is the best choice. It reduces time amount and generates clean code. Here, we are using scaffolding to develop a <a href=\"https:\/\/www.h2kinfosys.com\/blog\/crud-operations-db\/\" class=\"rank-math-link\">CRUD application<\/a>.<\/p>\n\n\n\n<p><strong>ASP.NET CRUD Example<\/strong><\/p>\n\n\n\n<p>This example consists of a couple of steps that are given below.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create New Project<\/li>\n<\/ol>\n\n\n\n<p>Select File menu from the menu bar and select New-&gt;Project. We can use the shortcut Ctrl+Shift+N also to create a new project.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/6O_hFWAYMWs-iCZuTupkAYt40LguvzT4gr0eXU9u3OY_RaDrCzMGY5BFLv7Q5I5h9Avbc_fDDGyz77py-2YKpSAxKpyFeaV9iHMAyTJBulw3FfSDmZnK0-K6XgFVCT-p_pZtILU\" alt=\"MVC Scaffolding\" title=\"\"><\/figure>\n\n\n\n<p>This will pop up a window containing projects. Here, we are selecting ASP.NET Web Application.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/L6be5Z40CbqXsPO7eQ89BmgDvYTLQO344FsYSD6VlGUVeiXKv1ti2eJ1upqSs01UeuryC18glUYMDgEwzr-xhyM_wpfCTbZfzUjcv94SCVCDyrJmTOyKrOGTUU5KASMk-rBIZvw\" alt=\"ASP Scaffolding 2\" title=\"\"><\/figure>\n\n\n\n<p>After clicking on ok, it pops up a new window of templates. Here, we are selecting the MVC template which is used to create an MVC web application.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/6Q2cZC0t4Kof-z8rYX1KWV9fL6ZQ3TkUTwJTwcwkMvgJhcqfooH0McX01q9vsLIJ8eaDu9vLDQH908vjvC-bFAO1TBMZYCISJwEEsWv2WxgXXHh8TJiAhsdi_PLbOVz5LxRi7LU\" alt=\"MVC Scaffolding\" title=\"\"><\/figure>\n\n\n\n<p>Hit ok, and then it will create a project and also shows a progress bar as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/UTScikJJGz_7YocR0nVguYiHIrDQe1w-G5fkP4OK5JjotnxYJsY4T2eQFCiWDfGlmMOJeOj0WDYzdNaxU2QAQVx1jD6a1QG48JnnJvhF5DE6sDJ64bXNfvURCZZujdiTAFvX_u8\" alt=\"ASP Scaffolding 4\" title=\"\"><\/figure>\n\n\n\n<p><strong>CRUD Project Structure:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/cO6LUuShFwZdxg93nfXJwAFaAKGBSdkfVr1PdgRJfJl8_pp6_jruN-zC4fQzc9bblfLJ6E4u0NMuHd0e3fghHHNd34fnAY6_pVZQGxNKdzSA7i7K9xfA8yK3dyQcF2Y1-vcqMRI\" alt=\"MVC Scaffolding\" title=\"\"><\/figure>\n\n\n\n<p>We can run the above application by pressing Ctrl+F5 that will produce a default index page to the browser and looks like the one below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/RBOPqwhvpNzLnVT-Db1sdR4NvJl73_WAkEsI7OBmtgkE1x7W4xVFYmImu3TsBZf5NKAaZyLTURQWYZnH5hs9BChRmQF8sAwp1sOsZ8bZ5s-zS57jLP0VaeIuLoEzsuoogAyRUqc\" alt=\"ASP Scaffolding 6\" title=\"\"><\/figure>\n\n\n\n<p>For creating complete crud, we need to add Models, Views, and Controllers in our project. Here, we are going to create a Model that deals with the data.<\/p>\n\n\n\n<p>1. Create a New Model<\/p>\n\n\n\n<p>We are now creating a Student Model inside the Models folder of our project. Right-click on the Models folder and select add-&gt;class that will pop up a dialog box. Create a class by providing a name.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/PPr5yGWsQ1dF8RlDNeVKdZlax6GEyM08T6lwqqx2cX518ymOM_SXp9jhinctYDxtIkixG5BiFniSIE_CdCKMx7YaBPZKV1OS9l8x-QBlZd1SGXIfZkJOG3eLv_bCM91G5cVfN8c\" alt=\"ASP Scaffolding 7\" title=\"\"><\/figure>\n\n\n\n<p>This model class has some source code, we can modify its code as given below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Student.cs\nusing System;&nbsp;&nbsp;\nusing System.Collections.Generic;&nbsp;&nbsp;\nusing System.ComponentModel.DataAnnotations;&nbsp;&nbsp;\nusing System.Linq;&nbsp;&nbsp;\nusing System.Web;&nbsp;&nbsp;\nnamespace CrudExample.Models&nbsp;&nbsp;\n{&nbsp;&nbsp;\npublic class Student&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\npublic int ID { get; set; }&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Required]&nbsp;&nbsp;\npublic string Name { get; set; }&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Required]&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[EmailAddress]&nbsp;&nbsp;\npublic string Email { get; set; }&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Required]&nbsp;&nbsp;\npublic string Contact { get; set; }&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n}&nbsp; &nbsp;\n<\/pre>\n\n\n\n<p>2. Create a Context Class<\/p>\n\n\n\n<p>We will now create another class inside the Models folder, and it is used to communicate with Entity Framework and perform <a href=\"https:\/\/www.omgwiki.org\/\" rel=\"nofollow noopener\" target=\"_blank\">database operations<\/a>. This class inherits DbContext class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ StudentRecord.cs\nusing System;&nbsp;&nbsp;\nusing System.Collections.Generic;&nbsp;&nbsp;\nusing System.Linq;&nbsp;&nbsp;\nusing System.Web;&nbsp;&nbsp;\nusing System.Data.Entity.ModelConfiguration.Conventions;&nbsp;&nbsp;\nusing System.Data.Entity;&nbsp;&nbsp;\nnamespace CrudExample.Models&nbsp;&nbsp;\n{&nbsp;&nbsp;\npublic class StudentRecord : DbContext&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\npublic DbSet&lt;Student&gt; Students { get; set; }&nbsp;&nbsp;\nprotected override void OnModelCreating(DbModelBuilder modelBuilder)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n}&nbsp;&nbsp;\n<\/pre>\n\n\n\n<p>3. Add Scaffold to the Project<\/p>\n\n\n\n<p>Right-click on the Controllers folder and add scaffold as we did in the screenshot.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/4wUj9L2-rV7hFCQvPYTzob6nroWGPGoOJmr56Rt218_TvkNO3Hhwq3j1yf-qUaOfNwPU6neyxTtaWIw-S9oViBBfXYYyvfDRZhtTOlRqlk1iVSkmDAFWAfi1bEKgBDVGKsx1JLY\" alt=\"ASP Scaffolding 8\" title=\"\"><\/figure>\n\n\n\n<p>It will pop up the below dialog box. Now, select controller with Entity Framework.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/kDnw8TCPwl-aff_UyjRh89R56NGvT2TtFbJh7Mph0Yt4tbaES-EUAqe91NbtLprlsVt-SBbcX5IiQVnFM8QAC1RYptyPMUFeaO-0fTlLVzCTX-44UE8y0wo9HyU2H3mmUMICmpk\" alt=\"ASP Scaffolding 9\" title=\"\"><\/figure>\n\n\n\n<p>And click Add button. It asks for the Model and context name. Fill in the entries and click ok.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/lXXKNNjKnK_mWcdXFFv6v7Jl37kddXrFyiucmlGMSi3Q-VyxQ7zt8yZoNzvwhPC_tdqXjTJjVudH-V5qGrsTTN-u46DJj0tARMw6Qg7xAwkRi0Qb3TCqT1DT5ydBN76KbmlEnjg\" alt=\"ASP Scaffolding 10\" title=\"\"><\/figure>\n\n\n\n<p>After clicking the add button, it creates a StudentsController controller and a Students folder. The Students folder contains web pages for each CRUD operation.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ StudentsController.cs\nusing System;&nbsp;&nbsp;\nusing System.Collections.Generic;&nbsp;&nbsp;\nusing System.Data;&nbsp;&nbsp;\nusing System.Data.Entity;&nbsp;&nbsp;\nusing System.Linq;&nbsp;&nbsp;\nusing System.Net;&nbsp;&nbsp;\nusing System.Web;&nbsp;&nbsp;\nusing System.Web.Mvc;&nbsp;&nbsp;\nusing CrudExample.Models;&nbsp;&nbsp;\nnamespace CrudExample.Controllers&nbsp;&nbsp;\n{&nbsp;&nbsp;\npublic class StudentsController : Controller&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nprivate StudentRecord db = newStudentRecord();&nbsp;&nbsp;\n\/\/ GET: Students&nbsp;&nbsp;\npublic ActionResult Index()&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nreturn View(db.Students.ToList());&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n\/\/ GET: Students\/Details\/5&nbsp;&nbsp;\npublic ActionResult Details(int? id)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nif (id == null)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nreturn new HttpStatusCodeResult(HttpStatusCode.BadRequest);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\nStudent student = db.Students.Find(id);&nbsp;&nbsp;\nif (student == null)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nreturn HttpNotFound();&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\nreturn View(student);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n\/\/ GET: Students\/Create&nbsp;&nbsp;\npublic ActionResult Create()&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nreturn View();&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n\/\/ POST: Students\/Create&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[HttpPost]&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ValidateAntiForgeryToken]&nbsp;&nbsp;\npublic ActionResult Create([Bind(Include = \"ID,Name,Email,Contact\")] Student student)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nif (ModelState.IsValid)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.Students.Add(student);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.SaveChanges();&nbsp;&nbsp;\nreturn RedirectToAction(\"Index\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\nreturn View(student);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n\/\/ GET: Students\/Edit\/5&nbsp;&nbsp;\npublic ActionResult Edit(int? id)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nif (id == null)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nreturn new HttpStatusCodeResult(HttpStatusCode.BadRequest);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\nStudent student = db.Students.Find(id);&nbsp;&nbsp;\nif (student == null)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nreturn HttpNotFound();&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\nreturn View(student);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n\/\/ POST: Students\/Edit\/5&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[HttpPost]&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ValidateAntiForgeryToken]&nbsp;&nbsp;\npublic ActionResult Edit([Bind(Include = \"ID,Name,Email,Contact\")] Student student)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nif (ModelState.IsValid)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.Entry(student).State = EntityState.Modified;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.SaveChanges();&nbsp;&nbsp;\nreturn RedirectToAction(\"Index\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\nreturn View(student);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n\/\/ GET: Students\/Delete\/5&nbsp;&nbsp;\npublic ActionResult Delete(int? id)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nif (id == null)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nreturn new HttpStatusCodeResult(HttpStatusCode.BadRequest);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\nStudent student = db.Students.Find(id);&nbsp;&nbsp;\nif (student == null)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nreturn HttpNotFound();&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\nreturn View(student);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n\/\/ POST: Students\/Delete\/5&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[HttpPost, ActionName(\"Delete\")]&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ValidateAntiForgeryToken]&nbsp;&nbsp;\npublic ActionResult DeleteConfirmed(int id)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nStudent student = db.Students.Find(id);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.Students.Remove(student);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.SaveChanges();&nbsp;&nbsp;\nreturn RedirectToAction(\"Index\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\nprotected override void Dispose(bool disposing)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\nif (disposing)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.Dispose();&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\nbase.Dispose(disposing);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n}&nbsp;\n<\/pre>\n\n\n\n<p>The Students folder available inside the View contains the below files.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/sAvf8JMKlBE3w43hGpP_YPICYULxvqgbwB-5ovpeW_YpSvt4L0e8l_YKztyZmybByWcnEzfCqpor783NdAGgfR2-MW0eFHNn-EWnPoXnFqA9IfLoyjwXWdon8bAQwPAWOE4r8_U\" alt=\"ASP Scaffolding 11\" title=\"\"><\/figure>\n\n\n\n<p>The Index.cshtml file contains the below code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Index.cshtml\n@model IEnumerable&lt;scaffoldingTest.Models.Student&gt;&nbsp;&nbsp;\n@{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;ViewBag.Title = \"Index\";&nbsp;&nbsp;\n}&nbsp;&nbsp;\n&lt;h2&gt;Index&lt;\/h2&gt;&nbsp;&nbsp;\n&lt;p&gt;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;@Html.ActionLink(\"Create New\", \"Create\")&nbsp;&nbsp;\n&lt;\/p&gt;&nbsp;&nbsp;\n&lt;table class=\"table\"&gt;&nbsp;&nbsp;\n&lt;tr&gt;&nbsp;&nbsp;\n&lt;th&gt;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.DisplayNameFor(model =&gt; model.Name)&nbsp;&nbsp;\n&lt;\/th&gt;&nbsp;&nbsp;\n&lt;th&gt;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.DisplayNameFor(model =&gt; model.Email)&nbsp;&nbsp;\n&lt;\/th&gt;&nbsp;&nbsp;\n&lt;th&gt;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.DisplayNameFor(model =&gt; model.Contact)&nbsp;&nbsp;\n&lt;\/th&gt;&nbsp;&nbsp;\n&lt;th&gt;&lt;\/th&gt;&nbsp;&nbsp;\n&lt;\/tr&gt;&nbsp;&nbsp;\n@foreach (var item in Model) {&nbsp;&nbsp;\n&lt;tr&gt;&nbsp;&nbsp;\n&lt;td&gt;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.DisplayFor(modelItem =&gt; item.Name)&nbsp;&nbsp;\n&lt;\/td&gt;&nbsp;&nbsp;\n&lt;td&gt;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.DisplayFor(modelItem =&gt; item.Email)&nbsp;&nbsp;\n&lt;\/td&gt;&nbsp;&nbsp;\n&lt;td&gt;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.DisplayFor(modelItem =&gt; item.Contact)&nbsp;&nbsp;\n&lt;\/td&gt;&nbsp;&nbsp;\n&lt;td&gt;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.ActionLink(\"Edit\", \"Edit\", new { id=item.ID }) |&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.ActionLink(\"Details\", \"Details\", new { id=item.ID }) |&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.ActionLink(\"Delete\", \"Delete\", new { id=item.ID })&nbsp;&nbsp;\n&lt;\/td&gt;&nbsp;&nbsp;\n&lt;\/tr&gt;&nbsp;&nbsp;\n}&nbsp;&nbsp;\n&lt;\/table&gt;&nbsp;\n<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Right-click on the Index.cshtml file and select &#8220;view in browser.&#8221; This will execute the file and produce the following output.<\/p>\n\n\n\n<p>\/\/ Index file<\/p>\n\n\n\n<p>This index file is used to show student records. Currently, the table is empty, so it does not show any data.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/W_WDdPh2oLakYUntCu7jebp40LzxdBMFitS4I4bJX17rjIzVU6Qp36jMlwRa_QbxJ7eWgelA3KO2rglTX7EK3JBvPIcv1WRlXlu46jRSJoJNwKQKH8IaNylqUFNnCLlehtpW5dU\" alt=\"ASP Scaffolding 12\" title=\"\"><\/figure>\n\n\n\n<p><strong>Add new Student<\/strong><\/p>\n\n\n\n<p>We can add a new student by clicking on the Create New button. This will redirect to a student form.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/6ritO8eFShpLuq3pSbYPe0IH-WHaiLT_FUBT_kSrn1aYANL4MzKjwxH2Ot3-mh9iL0BDx-rAKe939ntbrplH0Zu_cyr6tBXgqMHGBHr0NkNDitcM1l-15BgTYNWoFcLD-ydqZZw\" alt=\"ASP Scaffolding 13\" title=\"\"><\/figure>\n\n\n\n<p>After adding it, we added two more entries then redirect them back to the index file. Now, it contains three student records.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/i9qBgSglgqeorwh5_vMZDVW8kKeZsPXeK4NP4ve3075PzIuA07p7HlXBbdqobeLahwfnuIV7yb2OEttE_6cOTshbLHzdOztTIrhGFqtBS2mrdHw0KQGcjsfTzVfYnK-s947d3lA\" alt=\"ASP Scaffolding 14\" title=\"\"><\/figure>\n\n\n\n<p><strong>Update Record<\/strong><\/p>\n\n\n\n<p>We can update the record by clicking on the Edit button. This will redirect to the update form. The following screenshot shows the edit page.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/ugDtey7l00bps5I8CTZWQZn4liBXc6UsgTf5RJ_Tzhdmn2C610XMWotBDmPhw_c-Ft0sDxnepRIp50ccQD-uJ0Wc7sUAQtnucwPFeWXqKv_dlM1gYLECk5aGr1IbpzQ0lRwxXqc\" alt=\"ASP Scaffolding 15\" title=\"\"><\/figure>\n\n\n\n<p>After updating the record, the index page looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/ar_szdzG_1MBLwXGpr_W3kOwEczW-503CiPkAUigqihvLm5limE-PrgYpQpbuuwneEnNKKynmNXUskZqdk9dIy3HykDit3wMnzTYm4gQPgHj1AWYgD10W45hRot8Ck4GuikK7gg\" alt=\"ASP Scaffolding 16\" title=\"\"><\/figure>\n\n\n\n<p><strong>Delete Record<\/strong><\/p>\n\n\n\n<p>We can delete any record simply by clicking on the provided Delete link. Let&#8217;s delete Roman John from the table. A confirmation message will be displayed to the user for surety.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/AgmyzPNpb48Jo029lBOBhdzqETcsy1bZ14KhVDcr3LwFfOXsW8y2A81Tyi0ininpH8D_bJumONVPh-9NP9TVnqOpHioYy_xDe5i3siG6wbI_hSrx9bTizK9CHCY7xrvmrKgeWbM\" alt=\"ASP Scaffolding 17\" title=\"\"><\/figure>\n\n\n\n<p>After clicking on the Delete button, it redirects to the index page that contains the remaining records.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/x-2juIVc0mH_PBobrRb0fw-Cjsj8pl_yd6ywIWUiesyB7_uxLZE4OR-ZfUVX6kZZNzz8rzHwfwrYBxkkMFQCVOOzBi0QKflfRoZ-0FYDycK-_7Hyz6JTLRoWXKhVJt_tTk0qUZU\" alt=\"ASP Scaffolding 18\" title=\"\"><\/figure>\n\n\n\n<p>We can see that there are only two records are present.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MVC Scaffolding is a feature of ASP.NET that allows you to generate functional code rapidly. It is also known as a code generator framework and is pre-installed in Visual Studio 2013 and a higher version. To create a basic CRUD application, scaffolding is the best choice. It reduces time amount and generates clean code. Here, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9170,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[499],"tags":[],"class_list":["post-9167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/9167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=9167"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/9167\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/9170"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=9167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=9167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=9167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}