Rajesh Yadav

Rajesh Yadav

  • NA
  • 105
  • 8.4k

Do not want to redirect the same page after submit in MVC

Apr 18 2018 6:21 PM
Currently, partial is getting submitted using AJAX after submit button click in main view. But I do not want the same partial to redirect after submission. How can I get rid off this. I am new to MVC so do not have much ideas. I have simply returned the partials in [HttpPost] ActionResult. I think, this is not correct way of doing this. Please guide me.
 
Scripts for submit 
  1. $(document).ready(function () {  
  2. var url = '@Url.Action("Home")';  
  3. $('#myform').submit(function () {  
  4. if (!$(this).valid())  
  5. return; }  
  6. $.post(url, $(this).serialize(), function (response)  
  7. {  
  8. $('#mypartial').html(response);  
  9. $("#myModal").modal('show'); });  
  10. return false;  
  11. })  
  12. });  
Controller
  1. [HttpPost]  
  2. public ActionResult Home(ClsAA clsAA)  
  3. {  
  4. ModelState.Clear();  
  5. if (Id == "Z01")  
  6. {  
  7. return PartialView("~/Views/Home/_P.cshtml", clsAA);  
  8. }  
  9. else if (Id == "P02")  
  10. {  
  11. return PartialView("~/Views/Home/_I.cshtml", clsAA);  
  12. }  
  13. return PartialView(); }  
Main View
  1. <table>  
  2. <tr>  
  3. <th>@Html.DisplayNameFor(m => m.ProductName)</th>  
  4. <th>@Html.DisplayNameFor(m => m.ProductDetail)</th>  
  5. <th>@Html.DisplayNameFor(m => m.ProductCost)</th>  
  6. </tr>  
  7. @foreach (var item in Model)  
  8. {  
  9. <tr>  
  10. <td>@Html.DisplayFor(modelItem => item.ProductName)</td>  
  11. <td >@Html.DisplayFor(modelItem => item.ProductDetail)</td>  
  12. <td>@Html.DisplayFor(modelItem => item.ProductCost)</td>  
  13. <td>  
  14. <input class="search btn-default" type="button" value="Select" data-assigned="@item.ProductCode"/>  
  15. </td>  
  16. </tr>  
  17. }  
  18. </table>  
  19. <div id="MyReports">  
  20. @using (Html.BeginForm("Home""Home", FormMethod.Post, new { id = "myform" }))  
  21. {  
  22. <div id="mypartial">  
  23. </div> <button type="submit" id="submit">Run</button>  
  24. }  
  25. </div>  
This is how Partial loads
  1. $('.search').click(function () {  
  2. var id = $(this).data('assigned');  
  3. var route = '@Url.Action("PartialView", "Home")?id=' + id;  
  4. $('#mypartial').load(route) });  
  5. Partial View  
  6. <div class="P"> <div id="H" class="PHT">  
  7. @Html.TextBoxFor(m => m.txtH)  
  8. </div>  
  9. <div id="T" class="PHT">  
  10. @Html.TextBoxFor(m => m.txtT)  
  11. </div>

Answers (2)