1

I am working with two context classes that are connected to two different databases. But on my index view I need to get info from both databases and then apply the paging to the join result. To do so I define the following viewModel class:-

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

And the following repository method to populate the ViewMaodel object:-

 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;
 }

And finally I have the following Action method:-

 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);
 }

But the above action method will raise an error since I cannot apply order by and Ipagedlist to non-list object. So can anyone advice on how to solve my problem, which mainly is:-

  1. Join context classes from different databases.

  2. Apply the paging to the join results .

Thanks in adbacne for any help.

Fals
  • 6,774
  • 3
  • 21
  • 43
  • Take a look at this thread/answer: [https://stackoverflow.com/a/28386319/1361831](https://stackoverflow.com/a/28386319/1361831) – Tiramonium Sep 14 '17 at 13:55

1 Answers1

0

So, you have:

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

Add one more ViewModel:

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

And in your 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);
 }

And your View should be stronly typed from Pagedclass: @model YourProject.ViewModels.Pagedclass

EDITED:

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);
     }
Andrey Gubal
  • 3,451
  • 2
  • 17
  • 21
  • Thanks for the reply, but this will raise the following error on .OrderBy inside the action method ::- :-Error 7 'TMS.ViewModels.RackJoinList' does not contain a definition for 'OrderBy' and no extension method 'OrderBy' accepting a first argument of type 'TMS.ViewModels.RackJoinList' could be found (are you missing a using directive or an assembly reference?) C:\Users\Administrator\documents\visual studio 2012\Projects\TMS\TMS\Controllers\RackController.cs 34 33 TMS – Asp.netmvc Asp.netmvc Aug 09 '13 at 15:08
  • @Asp.netmvcAsp.netmvc Try to devide your query in Action into 2 parts. I've updated my answer. May be it will help – Andrey Gubal Aug 09 '13 at 15:22