Create a new MVC 4 Application, given below-





Create a table, using the script, given below (if needed,as table will be generated on code first)-

  1. CREATE TABLE [dbo].[StudentDetails] (
  2. [Id] INT IDENTITY (1, 1) NOT NULL,
  3. [StudentName] NVARCHAR (MAX) NULL,
  4. [Age] INT NULL,
  5. CONSTRAINT [PK_dbo.StudentDetails] PRIMARY KEY CLUSTERED ([Id] ASC)
  6. );
Add Connection string in Web.config, as given below-
  1. <connectionStrings>
  2. <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-BootstrapApp-20160910224720;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-BootstrapApp-20160910224720.mdf" providerName="System.Data.SqlClient" />
  3. </connectionStrings>
Add StudentDetails.cs and DataContext class file in Model, as given below-
  1. public class StudentDetails
  2. {
  3. public int Id { get; set; }
  4. public string StudentName { get; set; }
  5. public int? Age { get; set; }
  6. }
  7. public class UsersContext : DbContext
  8. {
  9. public UsersContext()
  10. : base("DefaultConnection")
  11. {
  12. }
  13. public DbSet<StudentDetails> studentDbset { get; set; }
  14. }
Add StudentController.cs with the codes, given below-
  1. public class StudentController : Controller
  2. {
  3. private UsersContext db = new UsersContext();
  4. //
  5. // GET: /Student/Student/
  6. public ActionResult Index()
  7. {
  8. return View(db.studentDbset.ToList());
  9. }
  10. public ActionResult Index1()
  11. {
  12. return View(db.studentDbset.ToList());
  13. }
  14. //
  15. // GET: /Student/Student/Details/5
  16. public ActionResult Details(int id = 0)
  17. {
  18. StudentDetails studentdetails = db.studentDbset.Find(id);
  19. if (studentdetails == null)
  20. {
  21. return HttpNotFound();
  22. }
  23. return View(studentdetails);
  24. }
  25. //
  26. // GET: /Student/Student/Create
  27. [HttpGet]
  28. public ActionResult Create()
  29. {
  30. return View();
  31. }
  32. //
  33. // GET: /Student/Student/Create
  34. public ActionResult CreateForm()
  35. {
  36. return View();
  37. }
  38. //
  39. // POST: /Student/Student/Create
  40. [HttpPost]
  41. public ActionResult Create(StudentDetails studentdetails)
  42. {
  43. if (ModelState.IsValid)
  44. {
  45. db.studentDbset.Add(studentdetails);
  46. db.SaveChanges();
  47. return RedirectToAction("Index");
  48. }
  49. return View(studentdetails);
  50. }
  51. //
  52. // GET: /Student/Student/Edit/5
  53. public ActionResult Edit(int id = 0)
  54. {
  55. StudentDetails studentdetails = db.studentDbset.Find(id);
  56. if (studentdetails == null)
  57. {
  58. return HttpNotFound();
  59. }
  60. return View(studentdetails);
  61. }
  62. public ActionResult EditForm(int id = 0)
  63. {
  64. StudentDetails studentdetails = db.studentDbset.Find(id);
  65. if (studentdetails == null)
  66. {
  67. return HttpNotFound();
  68. }
  69. return View(studentdetails);
  70. }
  71. //
  72. // POST: /Student/Student/Edit/5
  73. [HttpPost]
  74. public ActionResult Edit(StudentDetails studentdetails)
  75. {
  76. if (ModelState.IsValid)
  77. {
  78. db.Entry(studentdetails).State = EntityState.Modified;
  79. db.SaveChanges();
  80. return RedirectToAction("Index");
  81. }
  82. return View(studentdetails);
  83. }
  84. //
  85. // GET: /Student/Student/Delete/5
  86. public ActionResult Delete(int id = 0)
  87. {
  88. StudentDetails studentdetails = db.studentDbset.Find(id);
  89. if (studentdetails == null)
  90. {
  91. return HttpNotFound();
  92. }
  93. return View(studentdetails);
  94. }
  95. //
  96. // POST: /Student/Student/Delete/5
  97. [HttpPost, ActionName("Delete")]
  98. public ActionResult DeleteConfirmed(int id)
  99. {
  100. StudentDetails studentdetails = db.studentDbset.Find(id);
  101. db.studentDbset.Remove(studentdetails);
  102. db.SaveChanges();
  103. return RedirectToAction("Index");
  104. }
  105. protected override void Dispose(bool disposing)
  106. {
  107. db.Dispose();
  108. base.Dispose(disposing);
  109. }
  110. }
Put the script, given below, in _Layouts.cshtml-
  1. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  3. <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


Screen clipping is taken: 11-09-2016 00:01

Add Index.cshtml View in Student Folder and add the snippet, given below-
  1. @model IEnumerable<BootstrapApp.Models.StudentDetails>
  2. @{
  3. ViewBag.Title = "Index";
  4. }
  5. <h2>Index</h2>
  6. <div class="container-fluid">
  7. <h1>Small Grid</h1>
  8. <p>The following example will result in a 25%/75% split on small, medium and large devices. On extra small devices, it will stack (100% width).</p>
  9. <p>Resize the browser window to see the effect.</p>
  10. <div class="row">
  11. <div class="col-sm-3">
  12. <label for="StudentName">Student name</label>
  13. </div>
  14. <div class="col-sm-9">
  15. <label for="Age">Age</label>
  16. </div>
  17. </div>
  18. @foreach (var item in Model) {
  19. <div class="row">
  20. <div class="col-sm-3">
  21. <label for="StudentName">@item.StudentName</label>
  22. </div>
  23. <div class="col-sm-3">
  24. <label for="Age">@item.Age</label>
  25. </div>
  26. <div class="col-sm-3">
  27. <a href="/Student/Edit/@item.Id">Edit</a> |
  28. <a href="/Student/Details/@item.Id">Details</a> |
  29. <a href="/Student/Delete/@item.Id">Delete</a>
  30. </div>
  31. </div>
  32. }
  33. </div>
  34. <p>
  35. @Html.ActionLink("Create New", "Create")
  36. </p>
Add Create.cshtml and add the snippet, given below-
  1. @model BootstrapApp.Models.StudentDetails
  2. @{
  3. ViewBag.Title = "Edit";
  4. }
  5. <h2>Edit</h2>
  6. <div class="container">
  7. <h2>Form control: input</h2>
  8. <form action="/Student/Edit" method="post" novalidate="novalidate">
  9. <div class="form-group">
  10. <input id="Id" name="Id" type="hidden" value="@Model.Id">
  11. </div>
  12. <div class="form-group">
  13. <label for="Student Name">Student Name:</label>
  14. <input type="text" class="form-control" id="StudentName" name="StudentName" value="@Model.StudentName">
  15. </div>
  16. <div class="form-group">
  17. <label for="Age">Age:</label>
  18. <input class="form-control" id="Age" name="Age" type="number" value="@Model.Age">
  19. </div>
  20. <div class="form-group">
  21. <p>
  22. <input type="submit" value="Create" />
  23. </p>
  24. </div>
  25. </form>
  26. </div>
  27. s
  28. <div>
  29. @Html.ActionLink("Back to List", "Index")
  30. </div>
  31. @section Scripts {
  32. @Scripts.Render("~/bundles/jqueryval")
  33. }
The output is given below-



http://localhost:5474/Student

Screen clipping is taken: 11-09-2016 00:04



http://localhost:5474/Student/Create


Screen clipping is taken: 11-09-2016 00:04



http://localhost:5474/Student/Edit/1


Screen clipping is taken: 11-09-2016 00:05

Click here to download. (DB is attached with the solution).