Tri Setia

Tri Setia

  • 1.2k
  • 463
  • 22.4k

CRUD using ASP.NET MVC

Apr 2 2021 4:34 AM
Hai guys,, l trying to learn CRUD in Vs 2019 Mvc Aplication. I Get error in Update and Delete Record.
the id taken properly in the Url but the data not loaded on view AddOrEdit.cshtml. I'm not use EF for the CRUD operation.
 
I'm get error like this when the edit is click.
 
the error when delete click
 
this the controller
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Mvc;  
  6. using Microsoft.AspNetCore.Mvc.Rendering;  
  7. using Microsoft.EntityFrameworkCore;  
  8. using WebApplicationMvc.Data;  
  9. using WebApplicationMvc.Models;  
  10. using Microsoft.Extensions.Configuration;  
  11. using System.Data;  
  12. using System.Data.SqlClient;  
  13. using Microsoft.Data.SqlClient;  
  14. namespace WebApplicationMvc.Controllers  
  15. {  
  16. public class GuestBookController : Controller  
  17. {  
  18. private readonly IConfiguration _configuration;  
  19. public GuestBookController(IConfiguration configuration)  
  20. {  
  21. this._configuration = configuration;  
  22. }  
  23. // GET: GuestBook  
  24. public IActionResult Index()  
  25. {  
  26. DataTable dt = new DataTable();  
  27. using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_mvc")))  
  28. {  
  29. con.Open();  
  30. SqlCommand cmd = new SqlCommand("spGetAllGuestBook", con);  
  31. SqlDataAdapter adap = new SqlDataAdapter(cmd);  
  32. cmd.CommandType = CommandType.StoredProcedure;  
  33. adap.Fill(dt);  
  34. }  
  35. return View(dt);  
  36. }  
  37. // GET: GuestBook/AddOrEdit/  
  38. public IActionResult AddOrEdit(int? id)  
  39. {  
  40. GuestBookViewModel guestBookViewModel = new GuestBookViewModel();  
  41. if (id > 0)  
  42. guestBookViewModel = GetGuestBookByID(id);  
  43. return View(guestBookViewModel);  
  44. }  
  45. // POST: GuestBook/AddOrEdit  
  46. // To protect from overposting attacks, enable the specific properties you want to bind to.  
  47. // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.  
  48. [HttpPost]  
  49. [ValidateAntiForgeryToken]  
  50. public IActionResult AddOrEdit(int id, [Bind("Id,Name,Email,Message")] GuestBookViewModel guestBookViewModel)  
  51. {  
  52. if (ModelState.IsValid)  
  53. {  
  54. using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_mvc")))  
  55. {  
  56. con.Open();  
  57. SqlCommand cmd = new SqlCommand("spSaveOrEdit", con);  
  58. cmd.CommandType = CommandType.StoredProcedure;  
  59. cmd.Parameters.AddWithValue("Id", guestBookViewModel.Id);  
  60. cmd.Parameters.AddWithValue("Name", guestBookViewModel.Name);  
  61. cmd.Parameters.AddWithValue("Email", guestBookViewModel.Email);  
  62. cmd.Parameters.AddWithValue("Message", guestBookViewModel.Message);  
  63. cmd.ExecuteNonQuery();  
  64. }  
  65. return RedirectToAction(nameof(Index));  
  66. }  
  67. return View();  
  68. }  
  69. // GET: GuestBook/Delete/5  
  70. public IActionResult Delete(int? id)  
  71. {  
  72. GuestBookViewModel guestBookViewModel = new GuestBookViewModel();  
  73. guestBookViewModel = GetGuestBookByID(id);  
  74. return View(guestBookViewModel);  
  75. }  
  76. // POST: GuestBook/Delete/5  
  77. [HttpPost, ActionName("Delete")]  
  78. [ValidateAntiForgeryToken]  
  79. public IActionResult DeleteConfirmed(int id)  
  80. {  
  81. using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_mvc")))  
  82. {  
  83. con.Open();  
  84. SqlCommand cmd = new SqlCommand("spDeleteGuestBookByID", con);  
  85. cmd.CommandType = CommandType.StoredProcedure;  
  86. cmd.Parameters.AddWithValue("Id", id);  
  87. cmd.ExecuteNonQuery();  
  88. return RedirectToAction(nameof(Index));  
  89. }  
  90. }  
  91. [NonAction]  
  92. public GuestBookViewModel GetGuestBookByID(int? id)  
  93. {  
  94. GuestBookViewModel guestBookViewModel = new GuestBookViewModel();  
  95. using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_mvc")))  
  96. {  
  97. DataTable dt = new DataTable();  
  98. con.Open();  
  99. SqlDataAdapter adap = new SqlDataAdapter("spGetGuestBookByID", con);  
  100. adap.SelectCommand.CommandType = CommandType.StoredProcedure;  
  101. adap.SelectCommand.Parameters.AddWithValue("@Id", id);  
  102. adap.Fill(dt);  
  103. if (dt.Rows.Count == 1)  
  104. {  
  105. guestBookViewModel.Id = Convert.ToInt32(dt.Rows[0]["Id"].ToString());  
  106. guestBookViewModel.Name = dt.Rows[1]["Name"].ToString();  
  107. guestBookViewModel.Email = dt.Rows[2]["Email"].ToString();  
  108. guestBookViewModel.Message = dt.Rows[3]["Message"].ToString();  
  109. }  
  110. return guestBookViewModel;  
  111. }  
  112. }  
  113. }  
  114. }  
this the index.cshtml
  1. @model System.Data.DataTable  
  2. @{  
  3. ViewData["Title"] = "List Guest Book";  
  4. }  
  5. <div class="jumbotron bg-info">  
  6. <h1 class="text-center">@ViewData["Title"]</h1>  
  7. </div>  
  8. <p>  
  9. <div class="btn btn-light">  
  10. <a asp-action="AddOrEdit">Create New</a>  
  11. </div>  
  12. </p>  
  13. <table class="table table-bordered">  
  14. <thead class="thead-light text-center">  
  15. <tr>  
  16. <th>  
  17. Name  
  18. </th>  
  19. <th>  
  20. Email  
  21. </th>  
  22. <th>  
  23. Message  
  24. </th>  
  25. <th>  
  26. </th>  
  27. </tr>  
  28. </thead>  
  29. <tbody>  
  30. @for (int i = 0; i < Model.Rows.Count; i++)  
  31. {  
  32. <tr>  
  33. <td>  
  34. @Model.Rows[i]["Name"]  
  35. </td>  
  36. <td>  
  37. @Model.Rows[i]["Email"]  
  38. </td>  
  39. <td>  
  40. @Model.Rows[i]["Message"]  
  41. </td>  
  42. <td>  
  43. <a asp-action="AddOrEdit" asp-route-id="@Model.Rows[i]["Id"]">Edit</a> |  
  44. <a asp-action="Delete" asp-route-id="@Model.Rows[i]["Id"]">Delete</a>  
  45. </td>  
  46. </tr>  
  47. }  
  48. </tbody>  
  49. </table>  
this is the AddOrEdit.cshtml
  1. @model WebApplicationMvc.Models.GuestBookViewModel  
  2. @{  
  3. ViewData["Title"] = Model.Id==0?"Save Data":"Update Data";  
  4. }  
  5. <h1>@ViewData["Title"]</h1>  
  6. <hr />  
  7. <div class="row">  
  8. <div class="col-md-4">  
  9. <form asp-action="AddOrEdit" autocomplete="off">  
  10. <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  11. <input type="hidden" asp-for="Id" />  
  12. <div class="form-group">  
  13. <label asp-for="Name" class="control-label"></label>  
  14. <input asp-for="Name" class="form-control" />  
  15. <span asp-validation-for="Name" class="text-danger"></span>  
  16. </div>  
  17. <div class="form-group">  
  18. <label asp-for="Email" class="control-label"></label>  
  19. <input asp-for="Email" class="form-control" />  
  20. <span asp-validation-for="Email" class="text-danger"></span>  
  21. </div>  
  22. <div class="form-group">  
  23. <label asp-for="Message" class="control-label"></label>  
  24. <input asp-for="Message" class="form-control" />  
  25. <span asp-validation-for="Message" class="text-danger"></span>  
  26. </div>  
  27. <div class="form-group">  
  28. <input type="submit" value="Save Data" class="btn btn-primary" />  
  29. </div>  
  30. </form>  
  31. </div>  
  32. </div>  
  33. <div>  
  34. <a asp-action="Index">Back to List</a>  
  35. </div>  
  36. @section Scripts {  
  37. @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}  
  38. }  
this is the delete.cshtml
  1. @model WebApplicationMvc.Models.GuestBookViewModel  
  2. @{  
  3. ViewData["Title"] = "Delete Guest Book";  
  4. }  
  5. <h1>@ViewData["Title"]</h1>  
  6. <h3 class="alert-danger">Are you sure you want to delete this?</h3>  
  7. <div>  
  8. <h4>GuestBookViewModel</h4>  
  9. <hr />  
  10. <dl class="row">  
  11. <dt class = "col-sm-2">  
  12. @Html.DisplayNameFor(model => model.Name)  
  13. </dt>  
  14. <dd class = "col-sm-10">  
  15. @Html.DisplayFor(model => model.Name)  
  16. </dd>  
  17. <dt class = "col-sm-2">  
  18. @Html.DisplayNameFor(model => model.Email)  
  19. </dt>  
  20. <dd class = "col-sm-10">  
  21. @Html.DisplayFor(model => model.Email)  
  22. </dd>  
  23. <dt class = "col-sm-2">  
  24. @Html.DisplayNameFor(model => model.Message)  
  25. </dt>  
  26. <dd class = "col-sm-10">  
  27. @Html.DisplayFor(model => model.Message)  
  28. </dd>  
  29. </dl>  
  30. <form asp-action="Delete">  
  31. <input type="hidden" asp-for="Id" />  
  32. <input type="submit" value="Delete" class="btn btn-danger" /> |  
  33. <a asp-action="Index">Back to List</a>  
  34. </form>  
  35. </div>  

Answers (5)