我有3个表格如下: 工作:IdJob,描述,......
JobSkill:IdSkill,Id_Job,Title,Description,Level ...
JobRequirments:IdReq,Id_Job,Title,Description ...
(填写如下数据: 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 ///等等
在视图中,我按如下方式显示Jobs表中的所有作业:
@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>}
如何在主foreach循环的每次迭代中从其他2个表中提取数据?
我也有模特:
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; }
}
}
和控制器的相关部分:
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());
}
答案 0 :(得分:0)
该模型是IEnumerable<JobShop.Jobs>
的实例。
foreach (var item in Model)
每个项目都是JobShop.Jobs
的实例。因此,您可以像访问任何常规C#类一样访问JobShop.Jobs的属性。
foreach(var jobRequirements in item.JobRequirements)
和
foreach(var jobSkills in item.JobSkills)