net4u net4u - 3 months ago 19
ASP.NET (C#) Question

Displaying results of multiple tables in same view in mvc with razor

I have 3 tables as follows:
Jobs: IdJob, Description, ...

JobSkill: IdSkill, Id_Job, Title, Description, Level ...

JobRequirments: IdReq, Id_Job, Title, Description ...

(filled with data as follow:
1, Job1, Title1
2, Job2, Title 2
/////
1, 1, Skill1Job1
2, 1, Skill2Job1
3, 2, Skill1Job2
///
1, 1, Req1Job1
2, 1, Req2Job1
3, 2, Req1Job2
4, 2, Req2Job2
/// and so on

In a view I display all jobs from Jobs table as follows:

@model IEnumerable<JobShop.Jobs>

@{
ViewBag.Title = "Jobs Listing Index";
}

<div class="title-lines">
<h3 class="mt0">Available Jobs Listing</h3>
</div>
@if (Model.Count() > 0)
{
<div>
@{int i = 1;
foreach (var item in Model)
{
<div class="bordered @(i++ % 2 == 0 ? "highlighted" : "")">
<p>@item.IdJob</p>
<p>@item.Titlu</p>
<p>@item.DateAdd</p>
<div class="thumb">
<img src="@item.ImageSRC" />
</div>
<p class="">@item.Description</p>
<p class="">@item.Content</p>
<p class="">@item.Demand</p>
<p>@item.Address</p>
<!--Here I want another div for JobSkills
<div>
foreach (var item1 in WhatModel1)
{
<div class="JobSkill">
//Skill(s)
<p>@item1.Id_Job</p>
<p>@item1.Title</p>
<p>@item1.Level</p>
</div>
}
</div>
-->
<!--Here I want another div for JobsRequirments
<div>
foreach (var item2 in WhatModel2)
{
<div class="JobReq">
//Requirment(s)
<p>@item2.Id_Job </p>
<p>@item2.Requirment</p>
</div>
-->
}
</div>
</div>
}
}
</div>
}
else
{<p style="color: red">Sorry ! No record(s) found.</p>}


How can I pull data from the other 2 tables on each iteration of main foreach loop?

I also have the model:

namespace JobShop
{
using System;
using System.Collections.Generic;

public partial class Jobs
{
public Jobs()
{
this.JobRequirements = new HashSet<JobRequirements>();
this.JobSkills = new HashSet<JobSkills>();
}

public int IdJob { get; set; }
public Nullable<int> IdWho { get; set; }
public string User { get; set; }
public Nullable<System.DateTime> DateAdd { get; set; }
public string ImageSRC { get; set; }
public string Titlu { get; set; }
public string WhereWhat { get; set; }
public string Description { get; set; }
public string Content { get; set; }
public string Solicitare { get; set; }
public Nullable<System.DateTime> DateStart { get; set; }
public Nullable<System.DateTime> DateEnd { get; set; }
public string Orar { get; set; }
public string Address { get; set; }
public Nullable<double> Latitudine { get; set; }
public Nullable<double> Longitudine { get; set; }
public string ZIP { get; set; }

public virtual AspNetUsers AspNetUsers { get; set; }
public virtual ICollection<JobRequirements> JobRequirements { get; set; }
public virtual ICollection<JobSkills> JobSkills { get; set; }
}
}


and the relevant part of the controller:

namespace JobShop
{
public class JobsController : Controller
{
private JobShopEntities db = new JobShopEntities();

// GET: Jobs
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
//stuff for paging, searching and sorting
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}

ViewBag.CurrentFilter = searchString;
//
var jobs = db.Jobs.Include(j => j.AspNetUsers);
//Some search stuff
if (!String.IsNullOrEmpty(searchString))
{
jobs = jobs.Where(s => s.Titlu.Contains(searchString)
|| s.Description.Contains(searchString));
}
//
switch (sortOrder)
{
case "name_desc":
jobs = jobs.OrderByDescending(s => s.Titlu);
break;
case "Date":
jobs = jobs.OrderBy(s => s.DateAdd);
break;
case "date_desc":
jobs = jobs.OrderByDescending(s => s.DateAdd);
break;
default:
//jobs = jobs.OrderBy(s => s.Titlu);
jobs = jobs.OrderBy(s => s.DateAdd);
break;

}

//
int pageSize = 5;
int pageNumber = (page ?? 1);
return View(jobs.ToPagedList(pageNumber, pageSize));
//return View(jobs.ToList());
}

Answer

The model is an instance of IEnumerable<JobShop.Jobs>.

foreach (var item in Model)

Each item is an instance of JobShop.Jobs. So you can access the properties of JobShop.Jobs just like you would in any regular C# class.

foreach(var jobRequirements in item.JobRequirements)

and

foreach(var jobSkills in item.JobSkills)