Self explanatory question.
Between posts, the grid I have setup is retaining the HasSelection bit, even if the WebGrid has been re-loaded with new data. Therefore, the functionality I have wired into the physical selection of a WebGrid record runs, even though the user hasn't selected anything on the new resultset yet.
Thoughts?
WebGrid obtains the selected row thru the query string. by default, the query string field is row
like http://localhost/grid?row=2
Ideally, you would remove that query string field before posting back like so http://localhost/grid
If it is not possible, set WebGrid.SelectedIndex
to -1
instead.
Edit
Here are few ways to set WebGrid.SelectedIndex
:
@{
WebGrid grid = new WebGrid(Model);
grid.SelectedIndex = ViewBag.SelectedIndex;
@grid.GetHtml(
columns: grid.Columns(
...
)
)
}
public ActionResult Index(int? row)
{
ViewBag.SelectedIndex = (IsKeepSelection() ? row.GetValueOrDefault() : 0) - 1; //TODO: add bound checking
return View(People.GetPeople());
}
Or (I prefer the previous one though since it's easier to understand):
@{
WebGrid grid = new WebGrid(Model);
if(ViewBag.ClearSelection) {
grid.SelectedIndex = -1;
}
@grid.GetHtml(
columns: grid.Columns(
...
)
)
}
public ActionResult Index(int? row)
{
ViewBag.ClearSelection = IsClearSelection();
return View(People.GetPeople());
}