1

我正在使用连接到两个不同数据库的两个上下文类。但是在我的索引视图中,我需要从两个数据库中获取信息,然后将分页应用于连接结果。为此,我定义了以下 viewModel 类:-

public class RackJoinList
{
  public IEnumerable<TMSRack> Racks { get; set; } // from the first database
  public IEnumerable<Resource> Resources { get; set; } //from the second database
}

以及以下用于填充 ViewMadel 对象的存储库方法:-

 public RackJoinList AllFindRacks_j(string q,bool forautocomplete=false)
 {
   RackJoinList rjlist = new RackJoinList();
   var racks = from rack in tms.TMSRacks.Where(
                a => a.Technology.Tag.ToUpper()
                  .StartsWith(q.ToUpper()) || (q == null)
               )
               select rack;

    rjlist.Racks = racks.ToList();
    var resources = from resource in entities.Resources
                    join c in rjlist.Racks
                    on resource.RESOURCEID equals c.Technology.IT360ID
                    select resource;
    rjlist.Resources = resources;
    return rjlist;
 }

最后我有以下 Action 方法: -

 public ActionResult Index(string searchTerm=null, int page = 1)
 {
   var racks = repository.AllFindRacks_j(searchTerm)
                 .OrderBy(a=>a.Technology.Tag).ToPagedList(page, 5) ;

   return View(racks);
 }

但是上述操作方法会引发错误,因为我无法将 order by 和 Ipagedlist 应用于非列表对象。那么任何人都可以就如何解决我的问题提出建议,主要是: -

  1. 加入来自不同数据库的上下文类。

  2. 将分页应用于连接结果。

感谢您的帮助。

4

1 回答 1

0

所以你有了:

public class RackJoinList
{
  public IEnumerable<TMSRack> Racks { get; set; } // from the first database
  public IEnumerable<Resource> Resources { get; set; } //from the second database
}

再添加一个 ViewModel:

public class Pagedclass
{
public IPagedList<RackJoinList> mylist {get; set;}
}

在您的 ActionResult 中:

public ActionResult Index(string searchTerm=null, int page = 1)
 {
   var racks = repository.AllFindRacks_j(searchTerm)
                 .OrderBy(a=>a.Technology.Tag).ToPagedList(page, 5) ;

var model = new Pagedclass
{
mylist = racks
};

   return View(model);
 }

并且您的视图应该是从 Pagedclass 中严格输入的:@model YourProject.ViewModels.Pagedclass

编辑:

public ActionResult Index(string searchTerm=null, int page = 1)
     {
       var racks = repository.AllFindRacks_j(searchTerm);

      var _racks = racks.OrderBy(a=>a.Technology.Tag).ToPagedList(page, 5)

    var model = new Pagedclass
    {
    mylist = _racks
    };

       return View(model);
     }
于 2013-08-09T14:53:10.467 回答