Reputation: 129
I'm trying to list the items in 2 tables using the inner join. I think there is a problem in view. Please help me.
This is the error I get
The model item passed into the dictionary is of type 'PersonelGiriş_MVC.Models.ortaktablo', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[PersonelGiriş_MVC.Models.ortaktablo]'.
Model classes:
public class personel
{
public int id { get; set; }
[Required(ErrorMessage = "Bu alan boş geçilemez!")]
public string isim { get; set; }
[Required(ErrorMessage = "Bu alan boş geçilemez!")]
public string soyisim { get; set; }
[Required(ErrorMessage = "Bu alan boş geçilemez!")]
public string tcno { get; set; }
[Required(ErrorMessage = "Bu alan boş geçilemez!")]
[DataType(DataType.DateTime, ErrorMessage = "Lütfen doğum tarihinizi doğru bir şekilde giriniz.")]
public int dogumtarihi { get; set; }
[Required(ErrorMessage = "Bu alan boş geçilemez!")]
public string dogumyeri { get; set; }
[Required(ErrorMessage = "Bu alan boş geçilemez!")]
public string cinsiyet { get; set; }
[Required(ErrorMessage = "Bu alan boş geçilemez!")]
public string kangrubu { get; set; }
[Required(ErrorMessage = "Bu alan boş geçilemez!")]
public string adres { get; set; }
[Required(ErrorMessage = "Bu alan boş geçilemez!")]
[DataType(DataType.PhoneNumber, ErrorMessage = "Lütfen telefon numaranızı doğru bir şekilde giriniz.")]
public string telno { get; set; }
public string resimismi { get; set; }
public virtual List<saat> saatler { get; set; }
}
public class saat
{
public int saatid { get; set; }
public string girissaati { get; set; }
public string cikissaati { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd'/'MM'/'yyyy}")]
public DateTime? kayittarihi { get; set; }
//her giriş/çıkış saati yanlızca bir ait olabilir.
public virtual personel personel { get; set; }
}
public class ortaktablo
{
public string p_isim { get; set; }
public string p_soyisim { get; set; }
public string p_girissaati { get; set; }
public string p_cikissaati { get; set; }
public List<ortaktablo> toplamtablo { get; set; }
}
Controller:
var secim = (from saat_veri in db.saatler
join per_veri in db.personeller on saat_veri.personel.id equals per_veri.id
select new ortaktablo { p_isim = per_veri.isim, p_soyisim = per_veri.soyisim, p_girissaati = saat_veri.girissaati, p_cikissaati = saat_veri.cikissaati });
var Model = new ortaktablo { toplamtablo = secim.ToList() };
return View(Model);
View:
@model List<PersonelGiriş_MVC.Models.ortaktablo>
@if (Model != null)
{
foreach (var item in Model)
{
<tr class="info">
<td>@Html.DisplayFor(i => item.p_isim)</td>
<td>@Html.DisplayFor(i => item.p_soyisim)</td>
<td>@Html.DisplayFor(i => item.p_girissaati)</td>
<td>@Html.DisplayFor(i => item.p_cikissaati)</td>
</tr>
}
}
Upvotes: 0
Views: 583
Reputation: 2809
This is how you create the model you pass
var Model = new ortaktablo { toplamtablo = secim.ToList() };
so you need to useModel.toplamtablo
in your foreach because that is where your list are set
foreach (var item in Model.toplamtablo)
{
<tr class="info">
<td>@Html.DisplayFor(i => item.p_isim)</td>
<td>@Html.DisplayFor(i => item.p_soyisim)</td>
<td>@Html.DisplayFor(i => item.p_girissaati)</td>
<td>@Html.DisplayFor(i => item.p_cikissaati)</td>
</tr>
}
Upvotes: 1
Reputation: 62498
You are passing Single object as model to view while your view is strongly typed to collection. You need to change either view or in action to match the types. It looks like there will be a single object to be passed.
Change the view code to be like:
@model PersonelGiriş_MVC.Models.ortaktablo
@if (Model != null)
{
<tr class="info">
<td>@Html.DisplayFor(i => ip_isim)</td>
<td>@Html.DisplayFor(i => i.p_soyisim)</td>
<td>@Html.DisplayFor(i => i.p_girissaati)</td>
<td>@Html.DisplayFor(i => i.p_cikissaati)</td>
</tr>
}
Upvotes: 0