部分视图搜索结果在分页中不返回任何结果

时间:2015-08-14 07:05:45

标签: asp.net-mvc partial-views pagedlist

我有两个部分视图用于搜索结果,使用bootstrap的选项卡窗格显示在父视图中。

这是我的父视图:(仅Index.cshtml相关代码)

<form class="form">
    @using (Html.BeginForm("Index", "Home", FormMethod.Get))
    {  
        <div class=" form-group">
            @Html.TextBox("search", ViewBag.CurrentFilter as string, 
                new { @placeholder = "Enter class number", @class = "form-control" })
        </div>
        <div class="form-group">
            <input class="btn btn-success" type="submit" value="Search" id="btnclick" />
        </div>
    }
</form> 
<div class="tab-content col-xs-12">
    <div class="tab-pane fade in active" id="info">
        @if (Model.student_info.Count() != 0)
        {
            @Html.Partial("_studentinfo")
        }
        else
        {
            <h4 style="padding:5px;">No records found.</h4>
        }
    </div>
    <div class="tab-pane fade" id="grades">
        @if (Model.gradeperterm.Count() != 0)
        {
            @Html.Partial("_gradeterm")
        }
        else
        {
            <h4 style="padding:5px;">No records found.</h4>
        }
    </div>
</div>

然后,对于我的每个局部视图,我都放了pagedlistpager

@if (Model.student_info.Count > 1)
{ 
    @Html.PagedListPager(Model.student_info, 
        page =>Url.Action("Index", 
            new { page = Page, currentFilter = ViewBag.CurrentFilter}))
}

然后我的控制器看起来像这样:

Guid query = new Guid();
query = (from b in db.school 
         where b.class_nr == search 
         select b.class_id).FirstOrDefault();

if (search != null)
{
    page = 1;
}
else 
{
    search = currentfilter;
}

ViewBag.CurrentFilter = search;

int pageSize = 5;
int pageNumber = (page ?? 1);
var smodel = new schoolmodel
{
    student_info = db.student_info.Where(x => x.class_id == query)
        .OrderBy(x => x.recorded_at_date_time)
        .ToPagedList(pageNumber, pageSize),

    gradeperterm = db.gradeperterm.Where(x => x.class_id == query)
         .OrderBy(x => x.term)
         .ToPagedList(pageNumber, pageSize)
};

return View(patmodel);

当我搜索时,它会成功显示每个选项卡窗格的前五页,但是当我点击下一页时,它会显示&#34;未找到结果&#34;但我的搜索字符串和当前过滤器不为空,如我的网址所示。我怎样才能使它工作?我是全新的mvc。答案将非常感激。谢谢。

1 个答案:

答案 0 :(得分:0)

部分视图与Razor的片段类似,因此您需要使用如下所示传递部分视图的数据

@Html.Partial("_studentinfo",Model)

或者,如果您需要进行一些预处理,可以使用Child Action,以便它可以拥有自己的Controller Action,如图所示

@Html.Action("controller","action",new { id = 1, otherRouteParam = "pagekey" })

并让该动作在所述预处理之后呈现局部视图

public PartialViewResult Action(int id, string otherRouteParam){
   //Additional Processing like Get a PagedList Slice of Data
   return PartialView("_studentinfo", result);
}