Description: I have a model WorkOrder which contains WorkOrderForm model (start_date, end_date, organization), a list of models (the actual orders) and page parameters (page, page_size, page_count). The main idea is to display work orders, and because there is a large amount of them I have to filter the data; work orders get pulled from a database server (not local). On the initial View i prompt for start_date and end_date, I use DataType.Date and for organization i use string, this information get's stored in the model which I then pass in to the HttpPost. It extracts the data and displays it. Now, because there is A LOT of orders, I made costume pages to sort data, and I use 2 variables, page and page_size, which are displayed and can be set on the view after a valid WorkOrderForm was submitted.
Problem: The problem I am facing right now is that I can't seem to pass the 2 parameters page and page_size, from the view back to the controller at the same time. Both of them work but seem to reset each other, Example: I am on page 4, set page_size from 20 to 50, and it resets page to 1, this one is alright, but the main on is when I chose a page, it will reset the page_size to default (20). All the submitting has to happen inside Html.BeginForm() otherwise i lose the information stored in my model.
EDIT: now using PagedList.
New Problem: when I select a page it calls the [httpget], resetting the model and page size. I tried to implement it all in the Index, but failed miserably.
WorkOrder:
public class WorkOrder
{
[Key]
public int Id { get; set; }
public IPagedList<mymodel> view_list { get; set; }
public WorkOrderForm work_form { get; set; }
}
Controller:
[HttpGet]
public ActionResult Index(WorkOrder model)
{
var list = new List<mymodel>();
model.view_list = list.ToPagedList(1,1);
return View(model);
}
[HttpPost]
public ActionResult Index(WorkOrder model, int? page, int? page_size, string start_date, string end_date, string org)
{
var list = new List<mymodel>();
if (ModelState.IsValid)
{
using (OracleDbctx ctx = new OracleDbctx())
{
//database stuff
int pageNumber = (page ?? 1);
int pageSize = (page_size ?? 20);
model.view_list = list.ToPagedList(pageNumber, pageSize);
return View(model);
}
}
ModelState.AddModelError("", "Incorrect Information submitted");
return View();
}
Page Info submission in my view:
@:Page @(Model.view_list.PageCount < Model.view_list.PageNumber ? 0 : Model.view_list.PageNumber) of @Model.view_list.PageCount
@Html.PagedListPager(Model.view_list, page => Url.Action("Index", "WorkOrder",
new { page, ViewBag.page_size, start_date = Model.work_form.start_date, end_date = Model.work_form.end_date, org = Model.work_form.org }));
Question: How do I go about passing both of the parameters from the view and at the same time keep my model information, even if i am only choosing to update 1 parameter? If you have suggestions on possible work around's I would be happy to hear some ideas, even pointing me in a direction would be appreciated. If there might be something I am missing please do say.
New Question: How to ether get PagedList to call the [httppost] Index, or whats the best way to implement something like this in the default Index controller?
nameattribute, not the values of other buttons. You already have a model withpage_sizeandpage_countso bind form controls to them (e.g, a dropdownlist or radio buttons forpages_size) and have one submit button (and remove thepageandpage_sizeparameters from the methodDropDownListFor(DDLF) to let user choose page size considered better. Buttons to select a page can be substituted with dynamically generatedActionLinks or another DDLF bound forpageproperty.