I am trying to implement a very simple paging techniques in mvc 2.0
I have a customer model which looks like this
public class Customer
{
public string name{get;set;}
public string address{get;set;}
public string city{get;set;}
}
Now in controller index I have a dummy collection or list which I am passing to view like this
ActionResult index(int page=1)
{
List<Customer> col=new List<Customer>();
col.Add(new Customer(.....)
.....
ViewData["PagedList"]=col.skip((page-1)*pagesize)*take(pagesize);
return View();
}
Now in View I have a dropdown list
<select id="dropdown" onChange="getthis()">
<option>1</option>
<option>2</option>
<option>3</option> //This are page no hardcoded
<option>4</option>
<option>5</option>
</select>
Now inside foreach loop I am looping through all ViewData passed from controller and it works fine.
When user changes page no from dropdown I pass it to controller like this
function getthis()
{
var page=//get page no from dropdown
self.location="/Home/Index?page=" + page
}
this also fetches correct data but it resets the value back to 1.My question is how can I hold the dropdown selected value(page number) after page change?
Help will be much appreciated.Thanks
Ok after beating my head for around 1 days I finally changed the approach.I am now returning my object in JsonResult instead of ActionResult.
In view now making ajax call to this controller method like this
This way it works expectedly.