简体   繁体   English

ASP.NET MVC的多个参数和模型

[英]ASP.NET MVC multiple parameters and models

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 ). 说明 :我有一个模型WorkOrder包含WorkOrderForm模型( start_date, end_date, organization ),模型列表(实际订单)和页面参数( 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 . 在最初的View我提示输入start_dateend_date ,我使用DataType.Date ,对于organization我使用string ,此信息存储在模型中,然后传递给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. 现在,因为有很多订单,所以我制作了服装页面来对数据进行排序,并使用了两个变量pagepage_size ,它们在提交有效的WorkOrderForm之后可以在视图上显示和设置。

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. 问题 :我现在面临的问题是我似乎无法将2参数pagepage_size从视图同时传递回控制器。 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). 他们两人的工作,但似乎重置对方,例如:我在page 4,设置page_size从20到50,并重置page为1,这一个是好的,但主要的是,当我选择了一个page ,它将page_size重置为默认值(20)。 All the submitting has to happen inside Html.BeginForm() otherwise i lose the information stored in my model. 所有提交都必须在Html.BeginForm()内部进行,否则我将丢失存储在模型中的信息。

EDIT: now using PagedList. 编辑:现在使用PagedList。

New Problem : when I select a page it calls the [httpget] , resetting the model and page size. 新问题 :当我选择一个页面时,它会调用[httpget] ,从而重置模型和页面大小。 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? 问题 :即使我只选择更新1个参数,我如何继续从视图传递两个参数并同时保留我的模型信息? 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? 新问题 :如何以太获取PagedList来调用[httppost]索引,或者什么是在默认Index控制器中实现类似内容的最佳方法?

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();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM