Reputation: 525
I have two tables pagecontent and programs. I need to display data from both tables in my homepage view.
here is my HomeController:
public class HomeController : Controller
{
[HandleError]
public ActionResult Index()
{
using (var dbContext = new webcastEntities())
{
pagecontent homepageText = dbContext.pagecontents.OrderByDescending(o => o.pageID).FirstOrDefault();
IOrderedQueryable<programs_tbl> programList =
dbContext.programs_tbl.Where(q => q.closed != "DEL").OrderBy(o => o.startDate);
IQueryable<JoinedContent> results = dbContext.pagecontents
.Join(dbContext.programs_tbl,
t1 => t1.pageID,
t2 => t2.ID,
(t1, t2) =>
new JoinedContent(t1, t2));
return View(results.ToList());
}
}
}
and this is whats in my view:
@using gcus.com.ViewModels.ContentHolders
@model IEnumerable<JoinedContent>
@{
ViewBag.Title = "Home";
}
<div class="col-md-6" style="border-right: 2px solid #c0c0c0;">
@foreach (JoinedContent item in @Model)
{
//@Html.Raw(item.Value1.content)
@Html.Raw(item.PageContent.pageText)
}
</div>
and this is my class:
public class JoinedContent
{
public pagecontent PageContent { get; set; }
public programs_tbl ProgramsTable { get; set; }
public JoinedContent(pagecontent pageContent, programs_tbl programsTable)
{
PageContent = pageContent;
ProgramsTable = programsTable;
}
}
}
I was able to quickwatch debug and confirm that my data is going into the variables so pull data isnt the problem but being able to send data to my view is the issue. Its alway returning null.... PLEASE HELP!
Upvotes: 0
Views: 111
Reputation: 525
using the IEnumerable function on the classes, i ran two queries and combined them in a result then added that to the view and all worked.
Upvotes: 0
Reputation: 3361
You could create a new class object that has properties for both objects. You would then create an instance of it, set the properites, and pass that as your model. Or you could put each object in the ViewBag and access them through that. I would recommend the first option.
Upvotes: 3