简体   繁体   English

导航到下一页时丢失模型数据

[英]Losing model data when navigating to next page

I'm using paging with ASP.NET MVC, but I'm losing model data when navigating to next page. 我在ASP.NET MVC中使用分页,但是导航到下一页时丢失了模型数据。

Here is the code: 这是代码:

Partial view: 部分视图:

@using PagedList;

@using PagedList.Mvc;

@model Models.MyObject

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
<table class="table table-striped table-hover sortable">
<thead>
    <tr>
        <th width="240">@Model.HeaderNames[0]</th>
        <th width="240">@Model.HeaderNames[1]</th>
        <th width="240">@Model.HeaderNames[2]</th>
        <th width="240">@Model.HeaderNames[3]</th>
        <th width="120" class="no-sort"></th>
    </tr>
</thead>
<tbody>

    @foreach (var member in Model.PagedModelList)
    {
        <tr>
            <td><span class="sort-field-value">@member.Thead1</span></td>
            <td><span class="sort-field-value">@member.Thead2</span></td>
            <td><span class="sort-field-value">@member.Thead3</span></td>
            <td><span class="sort-field-value">@member.Thead4</span></td>
        </tr>
    }
</tbody>
</table>
<footer>
   <div>

    Page @(Model.PagedModelList.PageCount < Model.PagedModelList.PageNumber 
? 0 : Model.PagedModelList.PageNumber) of @Model.PagedModelList.PageCount

@Html.PagedListPager(Model.PagedModelList, page =>Url.Action("Reports",new { @page = page, FirstLoad = false }))
   </div>
</footer>

Controller : 控制器:

public ActionResult Reports(MyObject model, int? page, bool FirstLoad = true)
{
        model.pageSize = 4;
        model.pageNumber = (page ?? 1);

        if (FirstLoad)
        {
            // getting the data from database here 
            // ... code

            // assigning pagemodelist
            model.PagedModelList = model.ModelList.ToPagedList(model.pageNumber, model.pageSize);
        }

        // here sending the model and all is good
        return PartialView("_MyView", model);
    }

Model 模型

public class MyObject
{
    public int SelectedPeriod { get; set; }
    public List<SecondObject> ModelList = new List<Secondobject>();
    public IPagedList<Secondobject> PagedModelList ;

    public int pageSize { get; set; }
    public int pageNumber { get; set; }
}

Second model class: 第二类模特:

public class SecondObject
{
    public string Thead1 { get; set; }
    public string Thead2 { get; set; }
    public string Thead3 { get; set; }
    public string Thead4 { get; set; }
}

Expected to get next page but all I get is empty model which causes null reference when sending again to view from controller. 预计将获得下一页,但我得到的只是一个空模型,当再次从控制器发送视图时,将导致空引用。 What am I doing wrong here? 我在这里做错了什么?

I'm getting right data in model the first time, I show the table with correct data, then when clicking on next page I debug in the controller so I got empty model.PagedModelList , even some other empty model properties . 我第一次在模型中获取正确的数据,向表显示正确的数据,然后单击下一页时在控制器中调试,因此得到了空的model.PagedModelList ,甚至还有其他一些空的模型属性。

Any help appreciated 任何帮助表示赞赏

Maybe, try to pass your model in parameters like : 也许,尝试通过以下参数传递模型:

@Html.PagedListPager(Model.PagedModelList, page =>Url.Action("Reports",new {@model = Model, @page = page, FirstLoad = false }))

Edit : Another solution is to remove MyObject from parameters. 编辑:另一个解决方案是从参数中删除MyObject。 Load each time your data from your database. 每次从数据库加载数据。 You can follow the example in GitHub project 您可以按照GitHub项目中的示例进行操作

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

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