简体   繁体   中英

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.

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 .

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. Load each time your data from your database. You can follow the example in GitHub project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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