0

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?

9
  • Your buttons will only pass the value of their name attribute, not the values of other buttons. You already have a model with page_size and page_count so bind form controls to them (e.g, a dropdownlist or radio buttons for pages_size) and have one submit button (and remove the page and page_size parameters from the method Commented Jul 28, 2016 at 9:15
  • Multiple submit buttons in a form usually discouraged, a DropDownListFor (DDLF) to let user choose page size considered better. Buttons to select a page can be substituted with dynamically generated ActionLinks or another DDLF bound for page property. Commented Jul 28, 2016 at 9:36
  • @StephenMuecke could you elaborate please? I am not sure how to implement that. Commented Jul 28, 2016 at 10:24
  • The code to handle this properly is far too to include here. I recommend you consider using a package such as PagedList.MVC - Sorting, Filtering, and Paging Commented Jul 28, 2016 at 10:47
  • @StephenMuecke I describe that when I use PagedList it doesn't carry over my model information, I am not sure how to make it so when I change the page, the information the model contains in the view is reused. Commented Jul 28, 2016 at 12:23

1 Answer 1

1

Following implementation of the Index in the Controller worked

public ActionResult Index(WorkOrder model, int? page, int? page_size, string start_date, string end_date, string org)
        {
            if (model.work_form == null)
            {
                if (start_date != null && end_date != null && org != null)
                {
                    var form = new WorkOrderForm
                    {
                        start_date = start_date,
                        end_date = end_date,
                        org = org
                    };
                    model.work_form = form;
                }
            }

            if (model.work_form != null)
            {
                using (OracleDbctx ctx = new OracleDbctx())
                {
                    //do database stuff
                    //var list = database(query).ToList()

                    int pageNumber = (page ?? 1);
                    int pageSize = (page_size ?? 20);
                    int count = list.Count;
                    if (pageNumber - 1 > count / page_size)
                    {
                        pageNumber = 1;
                    }


                    model.view_list = list.ToPagedList(pageNumber, pageSize);

                    return View(model);
                }
            }

            return View();
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.