Mark Tabor

Mark Tabor

  • 572
  • 1.9k
  • 431k

how to load data as listing after saving it into the db mvc

Oct 14 2019 12:05 AM
I have a profile page in which i have an education section for that i have created a partial view and on button click i am opening the partial view and then saving the data but after saving it is not loading the grid with latest data i need to refresh the page . check the below code
  1. [HttpPost]  
  2. [ValidateAntiForgeryToken]  
  3. public ActionResult Create([Bind(Include = "ID,Degree_Level,Degree_Name,Institution_Attended,User_Id,BoardORUniversity,MarksObtained,TotalMarks,Year_Passed,Active,Date_Started,Date_End,Date_Created,Date_Modified")] Student_Experience student_Experience)  
  4. {  
  5. ViewBag.Colleges = _IEducation.PopulateColleges();  
  6. if (ModelState.IsValid)  
  7. {  
  8. db.Student_Experience.Add(student_Experience);  
  9. db.SaveChanges();  
  10. return RedirectToAction("Index");  
  11. }  
  12. return View(student_Experience);  
  13. }
My Partial view is below
  1. @model CMS_Monitoring.Models.Student_Experience  
  2. @{  
  3. /**/  
  4. Layout = null;  
  5. }  
  6. <!DOCTYPE html>  
  7. <html>  
  8. <head>  
  9. <meta name="viewport" content="width=device-width" />  
  10. <title>Create</title>  
  11. <script>  
  12. $(document).ready(function () {  
  13. // $(document.getElementById("sbmtInput").click(function(e){}  
  14. $('input[type=submit]').click(function (e) {  
  15. e.preventDefault();  
  16. $.ajax({  
  17. type: "POST",  
  18. url: '@Url.Action("Create", "Student_Experience")',  
  19. // dataType: "json",  
  20. data: $('form').serialize(),  
  21. success: function (result) {  
  22. // document.getElementById("demo1").innerHTML = "";  
  23. $(".demo1").html(result);  
  24. },  
  25. error: function () {  
  26. }  
  27. });  
  28. });  
  29. });  
  30. </script>  
  31. </head>  
  32. <body>  
  33. @using (Html.BeginForm())  
  34. {  
  35. <div class="form-horizontal">  
  36. @*<h4>Add Qualification</h4>  
  37. <hr />*@  
  38. @Html.ValidationSummary(true""new { @class = "text-danger" })  
  39. <div class="form-group">  
  40. @Html.LabelFor(model => model.Degree_Level, htmlAttributes: new { @class = "control-label col-md-2" })  
  41. <div class="col-md-10">  
  42. @Html.DropDownListFor(model=>model.Degree_Level, new List<SelectListItem>  
  43. {  
  44. new SelectListItem{ Text="Select Degree", Value = "0" },  
  45. new SelectListItem{ Text="High School", Value = "1" },  
  46. new SelectListItem{ Text="Intermediate", Value = "2" },  
  47. new SelectListItem{ Text="Bechelor's", Value = "3" },  
  48. new SelectListItem{ Text="Master's", Value = "4" },  
  49. new SelectListItem{ Text="Doctorate (PhD)", Value = "5" },  
  50. new SelectListItem{ Text="Vocational", Value = "6" },  
  51. new SelectListItem{ Text="Associate Degree", Value = "7" },  
  52. new SelectListItem{ Text="Certification", Value = "8" },  
  53. }, new { @class = "btn form-text", @id = "myid" })  
  54. @Html.ValidationMessageFor(model => model.Degree_Level, ""new { @class = "text-danger" })  
  55. </div>  
  56. </div>  
  57. <div class="form-group">  
  58. @Html.LabelFor(model => model.Degree_Name, htmlAttributes: new { @class = "control-label col-md-2" })  
  59. <div class="col-md-10">  
  60. @Html.EditorFor(model => model.Degree_Name, new { htmlAttributes = new { @class = "form-control" } })  
  61. @Html.ValidationMessageFor(model => model.Degree_Name, ""new { @class = "text-danger" })  
  62. </div>  
  63. </div>  
  64. <div class="form-group">  
  65. @Html.LabelFor(model => model.Institution_Attended, htmlAttributes: new { @class = "control-label col-md-2" })  
  66. <div class="col-md-10">  
  67. @Html.EditorFor(model => model.Institution_Attended, new { htmlAttributes = new { @class = "form-control" } })  
  68. @Html.ValidationMessageFor(model => model.Institution_Attended, ""new { @class = "text-danger" })  
  69. </div>  
  70. </div>  
  71. @*<div class="form-group">  
  72. @Html.LabelFor(model => model.User_Id, htmlAttributes: new { @class = "control-label col-md-2" })  
  73. <div class="col-md-10">  
  74. @Html.EditorFor(model => model.User_Id, new { htmlAttributes = new { @class = "form-control" } })  
  75. @Html.ValidationMessageFor(model => model.User_Id, ""new { @class = "text-danger" })  
  76. </div>  
  77. </div>*@  
  78. <div class="form-group">  
  79. @Html.LabelFor(model => model.BoardORUniversity, htmlAttributes: new { @class = "control-label col-md-2" })  
  80. <div class="col-md-10">  
  81. @Html.EditorFor(model => model.BoardORUniversity, new { htmlAttributes = new { @class = "form-control" } })  
  82. @Html.ValidationMessageFor(model => model.BoardORUniversity, ""new { @class = "text-danger" })  
  83. </div>  
  84. </div>  
  85. <div class="form-group">  
  86. @Html.LabelFor(model => model.MarksObtained, htmlAttributes: new { @class = "control-label col-md-2" })  
  87. <div class="col-md-10">  
  88. @Html.EditorFor(model => model.MarksObtained, new { htmlAttributes = new { @class = "form-control" } })  
  89. @Html.ValidationMessageFor(model => model.MarksObtained, ""new { @class = "text-danger" })  
  90. </div>  
  91. </div>  
  92. <div class="form-group">  
  93. @Html.LabelFor(model => model.TotalMarks, htmlAttributes: new { @class = "control-label col-md-2" })  
  94. <div class="col-md-10">  
  95. @Html.EditorFor(model => model.TotalMarks, new { htmlAttributes = new { @class = "form-control" } })  
  96. @Html.ValidationMessageFor(model => model.TotalMarks, ""new { @class = "text-danger" })  
  97. </div>  
  98. </div>  
  99. <div class="form-group">  
  100. @Html.LabelFor(model => model.Year_Passed, htmlAttributes: new { @class = "control-label col-md-2" })  
  101. <div class="col-md-10">  
  102. @Html.EditorFor(model => model.Year_Passed, new { htmlAttributes = new { @class = "form-control" } })  
  103. @Html.ValidationMessageFor(model => model.Year_Passed, ""new { @class = "text-danger" })  
  104. </div>  
  105. </div>  
  106. <div class="form-group">  
  107. @Html.LabelFor(model => model.Active, htmlAttributes: new { @class = "control-label col-md-2" })  
  108. <div class="col-md-10">  
  109. <div class="checkbox">  
  110. @Html.EditorFor(model => model.Active)  
  111. @Html.ValidationMessageFor(model => model.Active, ""new { @class = "text-danger" })  
  112. </div>  
  113. </div>  
  114. </div>  
  115. <div class="form-group">  
  116. @Html.LabelFor(model => model.Date_Started, htmlAttributes: new { @class = "control-label col-md-2" })  
  117. <div class="col-md-10">  
  118. @Html.EditorFor(model => model.Date_Started, new { htmlAttributes = new { @class = "form-control" } })  
  119. @Html.ValidationMessageFor(model => model.Date_Started, ""new { @class = "text-danger" })  
  120. </div>  
  121. </div>  
  122. <div class="form-group">  
  123. @Html.LabelFor(model => model.Date_End, htmlAttributes: new { @class = "control-label col-md-2" })  
  124. <div class="col-md-10">  
  125. @Html.EditorFor(model => model.Date_End, new { htmlAttributes = new { @class = "form-control" } })  
  126. @Html.ValidationMessageFor(model => model.Date_End, ""new { @class = "text-danger" })  
  127. </div>  
  128. </div>  
  129. @*<div class="form-group">  
  130. @Html.LabelFor(model => model.Date_Created, htmlAttributes: new { @class = "control-label col-md-2" })  
  131. <div class="col-md-10">  
  132. @Html.EditorFor(model => model.Date_Created, new { htmlAttributes = new { @class = "form-control" } })  
  133. @Html.ValidationMessageFor(model => model.Date_Created, ""new { @class = "text-danger" })  
  134. </div>  
  135. </div>  
  136. <div class="form-group">  
  137. @Html.LabelFor(model => model.Date_Modified, htmlAttributes: new { @class = "control-label col-md-2" })  
  138. <div class="col-md-10">  
  139. @Html.EditorFor(model => model.Date_Modified, new { htmlAttributes = new { @class = "form-control" } })  
  140. @Html.ValidationMessageFor(model => model.Date_Modified, ""new { @class = "text-danger" })  
  141. </div>  
  142. </div>*@  
  143. <div class="form-group">  
  144. <div class="col-md-offset-2 col-md-10">  
  145. <input type="submit" id="sbmtInput" value="Create" class="btn btn-default" />  
  146. @*@{Html.RenderAction("Index""Student_Experience");}*@  
  147. @Html.ActionLink("Cancel""Index""Student_Experience"nullnew { @class = "btn btn-dark active" })  
  148. </div>  
  149. </div>  
  150. </div>  
  151. }  
  152. @*<div>  
  153. @Html.ActionLink("Back to List""Index")  
  154. </div>*@  
  155. <br/><br/>  
  156. @Scripts.Render("~/bundles/jquery")  
  157. @Scripts.Render("~/bundles/bootstrap")  
  158. </body>  
  159. </html>  

Answers (1)