Итак, я работаю над проектом в ASP.NET MVC, в котором мне нужно создать веб-приложение, которое отображает информацию о школе и информацию об их сотрудниках в едином представлении.Я заканчиваю это, но это не работает, как я хотел. Просмотр , он показывает мне информацию о школе вместе с сотрудниками, но мне нужно отдельно, как это Как это должно быть .
Это коддля этого ПРОСМОТРА:
@model IEnumerable<ProjekatFinalni.Models.Zajedno>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.SkolaID)
</th>
<th>
@Html.DisplayNameFor(model => model.NazivSkole)
</th>
<th>
@Html.DisplayNameFor(model => model.AdresaRegistracije)
</th>
<th>
@Html.DisplayNameFor(model => model.Opstina)
</th>
<th>
@Html.DisplayNameFor(model => model.PostanskiBroj)
</th>
<th>
@Html.DisplayNameFor(model => model.MaticniBrojSkole)
</th>
<th>
@Html.DisplayNameFor(model => model.PIB)
</th>
<th>
@Html.DisplayNameFor(model => model.BrojRacunaSkole)
</th>
<th>
@Html.DisplayNameFor(model => model.WebStranica)
</th>
<th>
@Html.DisplayNameFor(model => model.Beleska)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.SkolaID)
</td>
<td>
@Html.DisplayFor(modelItem => item.NazivSkole)
</td>
<td>
@Html.DisplayFor(modelItem => item.AdresaRegistracije)
</td>
<td>
@Html.DisplayFor(modelItem => item.Opstina)
</td>
<td>
@Html.DisplayFor(modelItem => item.PostanskiBroj)
</td>
<td>
@Html.DisplayFor(modelItem => item.MaticniBrojSkole)
</td>
<td>
@Html.DisplayFor(modelItem => item.PIB)
</td>
<td>
@Html.DisplayFor(modelItem => item.BrojRacunaSkole)
</td>
<td>
@Html.DisplayFor(modelItem => item.WebStranica)
</td>
<td>
@Html.DisplayFor(modelItem => item.Beleska)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { / id=item.PrimaryKey / }) |
@Html.ActionLink("Details", "Details", new { / id=item.PrimaryKey / }) |
@Html.ActionLink("Delete", "Delete", new { / id=item.PrimaryKey / })
</td>
</tr>
}
</table>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Ime)
</th>
<th>
@Html.DisplayNameFor(model => model.Prezime)
</th>
<th>
@Html.DisplayNameFor(model => model.RadnoMesto)
</th>
<th>
@Html.DisplayNameFor(model => model.SkolaID)
</th>
<th>
@Html.DisplayNameFor(model => model.KontaktID)
</th>
<th>
@Html.DisplayNameFor(model => model.NazivSkole)
</th>
<th>
@Html.DisplayNameFor(model => model.AdresaRegistracije)
</th>
<th>
@Html.DisplayNameFor(model => model.Opstina)
</th>
<th>
@Html.DisplayNameFor(model => model.PostanskiBroj)
</th>
<th>
@Html.DisplayNameFor(model => model.MaticniBrojSkole)
</th>
<th>
@Html.DisplayNameFor(model => model.PIB)
</th>
<th>
@Html.DisplayNameFor(model => model.BrojRacunaSkole)
</th>
<th>
@Html.DisplayNameFor(model => model.WebStranica)
</th>
<th>
@Html.DisplayNameFor(model => model.Beleska)
</th>
<th></th>
</tr>
@foreach (var item2 in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item2.Ime)
</td>
<td>
@Html.DisplayFor(modelItem => item2.Prezime)
</td>
<td>
@Html.DisplayFor(modelItem => item2.RadnoMesto)
</td>
<td>
@Html.DisplayFor(modelItem => item2.SkolaID)
</td>
<td>
@Html.DisplayFor(modelItem => item2.KontaktID)
</td>
<td>
@Html.DisplayFor(modelItem => item2.NazivSkole)
</td>
<td>
@Html.DisplayFor(modelItem => item2.AdresaRegistracije)
</td>
<td>
@Html.DisplayFor(modelItem => item2.Opstina)
</td>
<td>
@Html.DisplayFor(modelItem => item2.PostanskiBroj)
</td>
<td>
@Html.DisplayFor(modelItem => item2.MaticniBrojSkole)
</td>
<td>
@Html.DisplayFor(modelItem => item2.PIB)
</td>
<td>
@Html.DisplayFor(modelItem => item2.BrojRacunaSkole)
</td>
<td>
@Html.DisplayFor(modelItem => item2.WebStranica)
</td>
<td>
@Html.DisplayFor(modelItem => item2.Beleska)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { / id=item.PrimaryKey / }) |
@Html.ActionLink("Details", "Details", new { / id=item.PrimaryKey / }) |
@Html.ActionLink("Delete", "Delete", new { / id=item.PrimaryKey / })
</td>
</tr>
}
</table>
И вот код для контроллера, который я использовал для VIEW:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjekatFinalni.Models;
namespace ProjekatFinalni.Controllers
{
public class KontaktController : Controller
{
// GET: Kontakt
public ActionResult Index(int skolaid)
{
BazaProjekatEntities bazaKontakt = new BazaProjekatEntities();
List<Kontakt> kontakti = bazaKontakt.Kontakt.Where(x => x.SkolaID == skolaid).ToList();
Zajedno zajednomodel = new Zajedno();
List<Zajedno> kontaktiskola = kontakti.Select(x => new Zajedno{ Ime = x.Ime, KontaktID = x.KontaktID, Prezime = x.Prezime ,RadnoMesto = x.RadnoMesto, NazivSkole = x.Skola.NazivSkole, AdresaRegistracije = x.Skola.AdresaRegistracije, Opstina=x.Skola.Opstina, SkolaID = x.SkolaID}).ToList();
return View(kontaktiskola);
}
}
}
Основная проблема в foreach, я не знаю, как с этим справиться, чтобы показать только одну школу, я имею в виду, мне вообще не нужен foreachдля этого, но без этого я не знаю, как.
С наилучшими пожеланиями!