Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am developing MVC4 application with Searching, Paging and Sorting using Ajax & Partial View. Search parameters are placed on parent View (Index.cshtml) and respective records are displayed in Partial View (_PartialView.cshtml) upon Search button click.

When I delete any record from grid, I want to reload partial view. I had already done this using returning PartialView from Delete action method. But if any searching parameter is selected, how can I access that value in Delete action method.

My delete link in _PartialView.cshtml

@Ajax.ActionLink("Delete", "Delete", new { SrNo = item.SrNo },
                                        new AjaxOptions { HttpMethod = "POST", Confirm = "Are you sure you want to delete record with SrNo : " + item.SrNo + "?", UpdateTargetId = "divData" }) 


In HomeController.cs

[HttpPost]
public ActionResult Delete(int SrNo)
{
List<UploadData> allUploadData;
.
.
.
return PartialView("_PartialView", allUploadData.ToPagedList(1, 100, TotalRecordsCount));  
}


Please suggest proper solution.

What I have tried:

I have tried using ViewBag, ViewData And TempData. In Index View, I stored all search parameters in ViewBag objects But in Partail View, all these ViewBag objects showing null values.
Posted
Updated 19-Sep-16 5:47am
Comments
Umesh AP 20-Sep-16 5:34am    
I managed to achieve same functionality using TempData as follows :

Whenever Search button clicks, I store search parameters in TempData variables as
TempData["srchByUserName"] = searchByUserName;
TempData["srchByUploadDate"] = searchByUploadDate;
TempData["currentPageIndex"] = currentPageIndex;



In Delete action, after reading these values use Keep() method to preserve values as :
TempData.Keep("srchByUserName");
TempData.Keep("srchByUploadDate");
TempData.Keep("currentPageIndex");


But the next problem is that whenever i delete records from other than 1st page, I redirect to 1st page.
I want that after deleting record from 2nd page, only 2nd page will get refreshed.

1 solution

So if you have edits like Start Date, End Date, and Search Term in some fashion such as

HTML
<input type="text" name="StartDate" id="StartDate">
<input type="text" name="EndDate" id="EndDate">
<input type="text" name="SearchTerm" id="SearchTerm">


And you want to access those values in your delete controller. You should adjust your delete button to be triggered via jquery using .ajax (or some equivalent).

You can do something like this on the click event of your delete button


JavaScript
$("#partial-div-id").on("click", ".delete-button-class", function() {
	var srNo = $(this).attr("data-srno");
	var startDate = $("#StartDate").val();
	var endDate = $("#EndDate").val();
	var searchTerm = $("#SearchTerm").val();
	
	 $.ajax({
		cache: false,
		url: '<path to="" your="" controller="" +="" action',<br="" mode="hold">		contentType: 'application/html; charset=utf-8',
		type: 'GET',
		dataType: 'html',
		data: { SrNo: srNo, StartDate: startDate, EndDate: endDate, SearchTerm: searchTerm  },
		async: false
	}).success(function (data) {
		$("#<your partial="" div="" id").html(data);<br="" mode="hold">	}).error(function () {
		// show an error message
	});

});</your></path>


Then you would adjust your Action in your contoller to have the params of start date, end date, and search term along with srno.

C#
[HttpPost]
public ActionResult Delete(int SrNo, DateTime StartDate, DateTime EndDate, string SearchTerm)
{
List<uploaddata> allUploadData;
.
.
.
return PartialView("_PartialView", allUploadData.ToPagedList(1, 100, TotalRecordsCount));  
}</uploaddata>


The jquery provided above expects a partial view to be returned in the success portion of the .ajax call so you would use the id of whatever it is that partial view is replacing to update the HTML in your application.
 
Share this answer
 
Comments
Umesh AP 20-Sep-16 2:10am    
@David_Wimbley - Thank you so much for response. I m trying with this approach but is their any other way of achieving the same without jQuery. I came across ViewDataDictionary while googling for the issue but can't find proper way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900