Reputation: 170
I am attempting to create a simple tool that pulls data from two databases and Active Directory. The issue I am struggling with is simply joining this data onto a single page.
I have been attempting any method I can think of to accomplish this, but ultimately what it comes down to is how do I populate data from another controller/context/model?
Perhaps I am over thinking it, this should be a very easy task (edit: easy for someone who prior to this didn't take the time to follow MVC). My current state:
For the sake of simplicity I'll disregard Active Directory for now. So, I have two controllers, two views, two contexts, and models for each table. They work fine separately. For the sake of simplicity the only common denominator is both data sources can be searched with the same search string, a unique identifier of an employee.
ConnectionDatabase tracks information about machines that connect to a service we use such as username, computer name, etc.
InventoryDatabase is an inventory management tool, human entered data.
The key piece of information here is at this point I'm simply trying to display data from both of those databases on one page. I'm not using joins, I'm not trying to correlate the data, or anything along those lines. Essentially if I could just take the View from Database1 and the View from Database2 and make them both show up in the same view that is frankly all I need. Two completely separate entities that just happen to appear on the same page.
A search function allows the user to search by a single sign on number. That number is stored in both of the databases and I'd like the results to be separate. For example, a left column that shows all the records from ConnectionDatabase matching SSO and a right column that shows all the records from InventoryDatabase matching SSO.
ConnectionDatabase
| - Table1
| - ClientName
| - SSO
| - Other Info
InventoryDatabase
| - Table1
| - Serial Number
| - SSO
Edit: When a controller is created it is my understanding a context is sent to it, therefore limiting it to a single context. The function GetConnectionAssets works perfectly because of course it is using the ConnectionContext AClient model.
Now, I want to toss in InventoryDatabase which has it's own InventoryDatabaseContext and InventoryDatabaseModel. Duplicating GetConnectionAssets and simply pointing it to InventoryDatabaseContext does not work due to the entire controller being dedicated to ConnectionContext.
In effort to explain this further, here is my controller for ~/Lookup/BySSO:
namespace MyApp.Controllers
{
public class BySSOController : Controller
{
private readonly ConnectionContext _context;
public BySSOController(ConnectionContext context)
{
_context = context;
}
public ActionResult PartialView()
{
return View();
}
public List<AClient> GetConnectionAssets(string SSO)
{
var aClients = from a in _context.AClient select a;
aClients = aClients.Include(a => a.AUserDefined)
.OrderByDescending(a => a.LastConnectDate);
if (!String.IsNullOrEmpty(SSO))
{
aClients = aClients.Where(a => a.AUserDefined.Sso.Equals(SSO))
.Include(a => a.AUserDefined);
}
return aClients.ToList();
}
public PartialViewResult RenderAClients(string SSO)
{
return PartialView(GetConnectionAssets(SSO));
}
}
}
Edit: I think a better way to describe this is I am building a dashboard. So, imagine an incident system dashboard that on a single page can show you metrics, currently open tickets, information about users, and some data potentially from an outside source. To the code itself, it may be data completely irrelevant to one another.
Edit: Since I'm having such a hard time explaining this and frustrating myself, let me put it together in a picture. I want this page to look exactly like this: http://imgur.com/a/tgZju
Except I'd like it to be in one browser window instead of two. :)
Upvotes: 0
Views: 419
Reputation: 239250
There's two ways you can handle this:
Utilize a view model. A view can only work with a single model, so the easiest solution is to create a new class that contains the data for both things you're trying to work with. Then, you pass this class to your view instead.
Utilize view components. Essentially, these are like mini actions. They operate in their own context, so you can issue separate queries to different databases, return different models and partial views, etc. Then, you just call each view component on your main view and let them do their thing.
Upvotes: 2