MVC3 WebGrid is holding onto it's paged data after a new grid is created via post!

StackOverflow https://stackoverflow.com/questions/6351209

  •  28-10-2019
  •  | 
  •  

Pergunta

I'm having an issue with WebGrid paging; but first, here's some controller pseudo code:

public ActionResult Date()
{
  var data = getLINQ();
  return View(data);
}

[HttpPost]
public ActionResult Date(string start, string finish)
{
   var data - getLINQ(start, finish);
   return View(data);
}

..and some view code:

@model IEnumerable<ModelClass>

@{
   ViewBag.Title = "Date";
   Layout = "~/Views/Shared/_Layout.cshtml";
   var grid = new WebGrid(source: Model, rowsPerPage: 25);
}


@using (Html.BeginForm())
{
<fieldset>
    <span>From date: <input type="text" id="start" name="start" data-val="true" value="@ViewBag.start" /></span> 
    <span>To date: <input type="text" id="finish" name="finish" data-val="true" value="@ViewBag.finish" /></span>
    <input type="submit" value="Update" />
</fieldset>
}

<div>
@grid.GetHtml(tableStyle: "table", caption: "Claims Between " + @ViewBag.start + " and " + @ViewBag.finish, htmlAttributes: new { cellspacing = "0", cellpadding = "0", id = "_grid" }, 
                        columns: grid.Columns(
                        grid.Column("ID", "ID"),
                        grid.Column("DateReceived", "Date Received"),
                        grid.Column("Client", "Client"),
                        grid.Column("ReferenceNumber", "Reference Number"),
                        grid.Column("PatientLastName", "Patient Last Name"),
                        grid.Column("PatientFirstName", "Patient First Name"),
                        grid.Column("ClaimType", "Type"),
                        grid.Column("BilledCharges", "Billed Charges", item => String.Format("{0:C}", item.BilledCharges ?? 0)),
                        grid.Column("ReimbursementAgent", "Reimbursement Agent")))
</div>

So, as you can decipher, the idea here is if a user GETS /Date, they are presented with a default set of data. If the user POSTS start and finish dates to /Date, the user is presented with that subset of data.

This is working great atm, save for one snare: If you GET the /Date, you have a paged resultset (page numbers under the grid), if you then POST and get new data, then click on any of the page numbers, the grid reloads with the original resultset and with the original paging!

Is there some way to reset that? Any help is appreciated!

Foi útil?

Solução

Ok - its not exactly what you want - but rather than fixing webgrid code, My recommendation when dealing with the WebGrid is to drop it in use of a far better control (imho)- the Telerik MVC grid. Its free, open source, and very easy to use. You can setup ajax paging with about two lines of code. Download it at:

http://telerikaspnetmvc.codeplex.com/

Demo: http://demos.telerik.com/aspnet-mvc/grid/

blog entry: http://msmvps.com/blogs/bmains/archive/2010/01/04/using-the-telerik-mvc-grid.aspx

Lots of info on the net - but the demo should show you its quite easy. There's no comparison as far as I'm concerned - and like I said - free and open source.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top