I lose all data on the second page, an there after.The first results/page shows up with correct data. The second page and there after, no data.
My code :
public ActionResult Contact(int? pageNumber, string search, string option, string select)
{
if (option == "Company")
{
return View(db.CompaniesServers.Where(x => x.Name.Contains(search)).ToList().ToPagedList(pageNumber ?? 1, 2));
}
else
{
return View(db.CompaniesServers.ToList().ToPagedList(pageNumber ?? 1, 3));
}
}
My View :
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<WorkingWithData.Models.CompaniesServer>
@using (Html.BeginForm("Contact", "Home", FormMethod.Get))
{
<b>
Search Options: <br />
</b>@Html.RadioButton("option", "Company") <text>Company Name</text>
@Html.TextBox("search", null, new { style = "height:30px" }) <select id="serverList" style="height:33px; font-size:small" class="btn btn-default">
<option value="All">Select Server</option>
<option value="10.67.21.40">Lam Server 4</option>
<option value="10.67.21.47">Lam Server 14</option>
<option value="10.67.21.70">Lam Server 12</option>
</select> <a class="btn btn-default" href="/Home/Contact">Reset</a> <input type="submit" name="submit" value="Search" class="btn btn-primary" />
}
<table class="table">
<tr>
<th>
Address
</th>
<th>
Port
</th>
<th>
Name
</th>
<th>
Environment
</th>
<th>
Active
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.Port)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Environment)
</td>
<td>
@Html.DisplayFor(modelItem => item.Active)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
@Html.PagedListPager(Model, pageNumber => Url.Action("Contact", new
{
pageNumber
}))
The first page results return valid info. But as soon as I go into the second page, nothing appears. My variables return null when debugging.
Should I do try and stop a page load? New with MVC though.
My workaround is to use a ViewBag in the controller, im not sure how you could implement it in your code but may help you
View:
@model IPagedList<MyModel>
<div class="pagedList">
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, search = ViewBag.search }),
PagedListRenderOptions.MinimalWithItemCountText)
</div>
Controller:
public ActionResult Index(string search = null, int page = 1)
{
ViewBag.search = search;
var model = _db.Employees
.Where(r => search == null || r.Name.StartsWith(search) || r.Name.Contains(search))
.Select(r => new EmployeeViewModel
{
Id = r.Id,
Name = r.Name,
City = r.City,
Country = r.Country,
}).ToPagedList(page, 10);
return View(model);
}
I found the answer. I really didn't want to use ViewBag
, but I had to. Seems I needed to store/send back my 'option' and 'search' values back, with every paging. At first, I was just storing the 'pageNumber' only.
Added in my controller:
ViewBag.Search1 = option;
ViewBag.Search2 = search;
Into view, added on the paging section of PagedListPager
:
option = ViewData["Search1"];
search = ViewData["Search2"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With