2

I have _LayoutOnecs.html file and View Loads with in it renderbody and displays list of records in table. in one of table column i have a Delete icon on click of it goes to controller and deletes record from database once the record is deleted the view should be refreshed so i returned the action to controller which fetches all the records

public ActionResult GetAdvertisementDetails() { var advertisementList = new AdvertisementManager().GetAdvertisementDetails(); return View("AdvertisementDetails", advertisementList); } public ActionResult DeleteAdvertisementDetails(int id) { new AdvertisementManager().DeleteAdvertisementDetails(id); return RedirectToAction("GetAdvertisementDetails", "Advertisement"); } 

once Delete is done it is going to GetAdvertisementcontroller and return view but the Deleted record sits there in table if i Refresh the page by pressing F5 the record is removed from table. How do i Refresh automatically when the record is deleted

View Code

<div class="col-md-12 main_table"> <div class="table-responsive"> <table class="table" id="hom_table"> <tr> <th>Advertisement Name</th> <th>Link</th> <th>Start Date</th> <th>End Date</th> <th width="100">Action</th> </tr> @foreach (var advertisements in Model) { <tr> <td> @advertisements.Description</td> <td> @advertisements.ImageUrl</td> <td> @advertisements.StartDate</td> <td> @advertisements.EndDate</td> <td> <a onclick="EditadvertisementDetails(@advertisements.AdvertisementId)"> <i class=" pull_Advt_details tbl_edit_btn fa fa-edit Editbutton"></i> </a> <a id="Deladvertisement" onclick="Deleteadvertisement(@advertisements.AdvertisementId)" > <i class="tbl_del_btn fa fa-trash"></i> </a> </td> </tr> } </table> </div> <!-- Responsive main table Ends --> </div> 
7
  • Provide implementation of GetAdvertisementDetails() as well as View code. Commented Apr 8, 2015 at 11:34
  • You last (now deleted) question indicated you were using ajax to call this method - is that still the case? Commented Apr 8, 2015 at 11:35
  • I am using ajax to call delete methode like this..function Deleteadvertisement(AdvertisementId) { GetServerData('Advertisement/DeleteAdvertisementDetails/' + AdvertisementId, null, null); } Commented Apr 8, 2015 at 11:39
  • dont use ajax call make simple call either by jquery or javascript Commented Apr 8, 2015 at 11:42
  • 2
    So why did you delete the last question? Ajax calls stay on the same page they do NOT redirect so return RedirectToAction(...) is pointless. Why would you degrade performance by redirect anyway instead of just removing the relevant advertisementfrom the view. If you do want poor performance then do a normal submit (include a form element and submit button for each advertisement) Commented Apr 8, 2015 at 11:44

1 Answer 1

2

Ajax calls stay on the same page. Your use of return RedirectToAction("GetAdvertisementDetails", "Advertisement"); in the controller method is ignored. Its also unnecessary to redirect since you can just remove the row from the table

Modify the html to (note remove the id attribute which is generating invalid html):

<a class="delete" data-id="@advertisements.AdvertisementId> <i class="tbl_del_btn fa fa-trash"></i> </a> 

and use the following script to call the controller method and remove the row

var url = '@Url.Action("DeleteAdvertisementDetails", "Advertisement")'; $('.delete').click(function() { var row = $(this).closest('tr'); $.post(url, { id: $(this).data('id') }, function(response) { if(response) { row.remove(); } }).fail(function (response) { // display error message? }); }); 

and modify the controller to

[HttpPost] public JsonResult DeleteAdvertisementDetails(int id) { new AdvertisementManager().DeleteAdvertisementDetails(id); return Json(true); } 
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the reply but in console i am getting error localhost/Membership/Advertisement/… @Stephen Muecke
What error are you getting? (and whats "Membership"? - are you using areas?)
localhost/Membership/Advertisement/… Failed to load resource: the server responded with a status of 404 (Not Found)
Again, what is "Membership"? Are you using areas? (404 means the method is not found so you need to specify the correct - I have only based it on what you have provided in your question)
Membership is name of the solution for other controllers i am getting url localhost/Membership/Advertisement/GetAdvertisementDetails but for this the url is localhost/Membership/Advertisement/… if url is localhost/Membership/Advertisement/DeleteAdvertisementDetails it will go to the controller
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.