Add below action in page

  1. public ActionResult GetDetails()
  2. {
  3. var data = DB.tblStuds.ToList();
  4. return PartialView(data);
  5. }

Add partial view for this action. RightClick on above Action > add View > Enter ViewName Same as action name > tick Create Partial View


Now we have index.cshtml where we have Show Data Button for which I have ajax call for getting stud details.

Index.cshtml

  1. @model projStudentInfo.Models.tblStud
  2. @ {
  3. ViewBag.Title = "Index";
  4. Layout = "~/Views/Shared/_Layout.cshtml";
  5. }
  6. < h2 >
  7. Student Info < /h2>
  8. @using (Html.BeginForm("Save", "Studs", FormMethod.Post))
  9. {
  10. <table>
  11. <tr>
  12. <td>
  13. Name
  14. </td >
  15. < td > @Html.TextBoxFor(c = > c.stud_name)
  16. < /td>
  17. </tr >
  18. < tr >
  19. < td >
  20. Age
  21. < /td>
  22. <td>@Html.TextBoxFor(c => c.stud_age)
  23. </td >
  24. < /tr>
  25. <tr>
  26. <td>
  27. <input type="submit" name="submit" value="Save" / >
  28. < /td>
  29. <td>
  30. <input type="button" name="submit" value="Show Data" id="btnShow" / >
  31. < /td>
  32. </tr >
  33. < /table>
  34. }
  35. <div id="dvShow"></div >
  36. < script >
  37. /* we have ajax call for calling GetDetails which will return html data in vHtml variable */
  38. $(document).ready(function() {
  39. $("#btnShow").click(function() {
  40. $.ajax({
  41. url: '@Url.Action("GetDetails","Studs")',
  42. type: 'post',
  43. success: function(vHtml) {
  44. $("#dvShow").html("");
  45. $("#dvShow").html(vHtml);
  46. }
  47. });
  48. });
  49. });
  50. < /script>
  51. GetDetails.cshtml (partialview)
  52. @model IEnumerable<projStudentInfo.Models.tblStud>
  53. <table border="0" cellpadding="0" cellspacing="0" id="tblStud">
  54. <thead>
  55. <tr>
  56. <th>
  57. Id
  58. </th >
  59. < th >
  60. Student Name
  61. < /th>
  62. <th>
  63. Age
  64. </th >
  65. < /tr>
  66. </thead >
  67. @foreach(var item in Model)
  68. {
  69. < tr >
  70. < td > @item.studid
  71. < /td>
  72. <td>@item.stud_name
  73. </td >
  74. < td > @item.stud_age
  75. < /td>
  76. </tr >
  77. }
  78. < /table>
  79. <style>
  80. #tblStud td {padding:5px;}
  81. #tblStud th {background:#aea;}
  82. </style >

Final Screen