当我在模型中绑定两个列表时如何使用Pagedlist.Mvc应用分页 [英] How To Apply Pagining Using Pagedlist.Mvc When I Have Two List Bind In The Model
问题描述
我有一个mvc应用程序,我从控制器返回两个列表到视图。一个列表是客户端,其他列表是产品。我希望在产品列表中分页和客户端列表将绑定到过滤器功能的复选框。现在,当我使用分页列表.mvc应用分页时,由于两个列表在视图中是绑定,因此它不应用分页。这是我的代码。
型号:
Hi,
i have a mvc application where i return two list from the controller to the view. one list is of Clients and other list is products. i want paging in product list anb client list will be bind to check box for filter functionality. now when i apply paging using paged list .mvc it do not apply paging because of the two lists are bind in the view. here is my code .
MODEL :
public class Clients
{
public int clientID;
public string name;
public string email;
public string phoneNumber;
}
public class Projects
{
public int ProjectID;
public string Title;
public int SourceLanguage;
public int RootFolder;
public int WordCount;
public int client;
public string trackStatus;
public string invoice;
public string trackPath;
public string ClientName;
}
public class ProjectClientList
{
public List<Clients> ClientList { get; set; }
public List<Projects> ProjectList { get; set; }
}
控制器:
Controller :
[HttpGet]
public ActionResult ListProduct(int Page = 1 , int PageSize = 2)
{
Clients objClient = new Clients();
List<Clients> ClientList = new List<Clients>();
ClientList = DataAccess.GetAllClients();
List<Projects> ProjectList = new List<Projects>();
ProjectList = DataAccess.GetAllProjects();
ProjectClientList objList = new ProjectClientList();
objList.ProjectList = ProjectList;
objList.ClientList = ClientList;
List<ProjectClientList> viewModelList = new List<ProjectClientList>();
viewModelList.Add(objList);
PagedList.PagedList<ProjectClientList> pagedData = new PagedList.PagedList<ProjectClientList>(viewModelList, Page, PageSize);
return View(pagedData.ToPagedList(Page,PageSize));
}
查看:
view :
@model PagedList.IPagedList<Strom.Models.ProjectClientList>
@using PagedList.Mvc;
<div class="col-md-2" style="border: 1px solid #dadada; padding: 0px;">
<h3 class="top-heading">Filters </h3>
<p style="color: 3c6a21rgb(60, 106, 33); padding: 7px; margin: 0px ! important; font-size: 18px;">Company:</p>
<div class="list-group">
@foreach (var item in Model)
{
if (@item.ClientList != null && @item.ClientList.Count > 0)
{
for (int k = 0; k < @item.ClientList.Count; k++)
{
<a href="#" class="list-group-item"> <input type="checkbox" > @item.ClientList[k].name </a>
}
}
}
</div>
</div>
<div id="DivList" class="row">
@foreach (var item in Model)
{
if (@item.ProjectList != null && @item.ProjectList.Count > 0)
{
for (int k = 0; k < @item.ProjectList.Count; k++)
{
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<input type="hidden" id="hdClientID" value="@item.ProjectList[k].client" />
<h3 class="top-heading">@item.ProjectList[k].ClientName </h3>
<div class="caption">
<h4 class="pull-right">
Quot: 595.68<br />
Cos: 433.90
</h4>
<h4>
<a href="#">@item.ProjectList[k].Title</a>
</h4>
</div>
<div class="ratings">
<p class="pull-right">Due 11/01/14<br>PM : Alison Fischer</p>
<p>
<span>Documents (0/1) </span><br>
<span>Documents (0/1) </span>
</p>
</div>
</div>
</div>
}
}
}
</div>
@Html.PagedListPager(Model,
page => Url.Action("ListProduct", new { page }))
推荐答案
创建一个viewModel类并将所有客户端和Project属性合并为一个,然后使用linq加入ProjectList和ClientList并选择进入viewmodel
Create a viewModel class and Merge all the client and Project properties in to one and then join the ProjectList and ClientList using linq and select in to viewmodel
public class ProjectClientViewModel{
public int clientID;
public string name;
public string email;
public string phoneNumber;
public int ProjectID;
public string Title;
public int SourceLanguage;
public int RootFolder;
public int WordCount;
public int client;
public string trackStatus;
public string invoice;
public string trackPath;
public string ClientName;
}
var finaResult=join query.Select(model=>new ProjectClientViewModel{})
最后绑定finalResult to PagedList
希望这有助于
finally bind the finalResult to PagedList
Hope this helps
这篇关于当我在模型中绑定两个列表时如何使用Pagedlist.Mvc应用分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!