如何从视图中访问 FK 值?
How do I access FK values from view?
我有两个模型。我如何从视图访问 FK 值或我应该如何去做? HTML 页面没有任何内容。
模型 1:
public class Ogrenci
{
public int OgrenciID { get; set; }
public int OgrenciNumarasi { get; set; }
public string Ad { get; set; }
public string Soyad { get; set; }
public string Bolum { get; set; }
public short Sinif { get; set; }
public string Yetenekler { get; set; }
public string Sifre { get; set; }
public List<Proje> Projeler { get; set; }
}
模型 2:
public class Proje
{
public int ProjeID { get; set; }
public string ProjeAdi { get; set; }
public string Aciklama { get; set; }
public DateTime EklenmeTarihi { get; set; }
public int OgrenciID { get; set; }
public Ogrenci Ogrenci { get; set; }
}
控制器:
public ActionResult Index()
{
return View(db.Projeler.ToList());
}
查看:
@model IEnumerable<DonemProjesi.Models.Proje>
@{
ViewBag.Title = "Index";
}
@Html.DisplayFor(modelItem => item.Ogrenci.Ad) //the problem is here
尝试指定要包含在查询结果中的相关对象:
return View(db.Projeler.Include(o => o.Ogrenci).ToList());
试试这个
public ActionResult Index()
{
return View(db.Ogrenci.Include(o => o.Projeler).FirstOrDefault());
}
@model DonemProjesi.Models.Ogrenci
@{
ViewBag.Title = "Index";
}
@Html.DisplayFor(m => m.Ad)
@for (int i = 0; i < Model.Projeler.Count; i+=1)
{
@Html.DisplayFor(model =>model.Projeler[i].ProjeAdi)
}
或者你是这个意思
public ActionResult Index()
{
return View(db.Projeler.Include(o => o.Ogrenci).ToList());
}
@model IEnumerable<DonemProjesi.Models.Proje>
@{
ViewBag.Title = "Index";
}
@for (int i = 0; i < Model.Count; i+=1)
{
@Html.DisplayFor(model =>model[i].ProjeAdi)
@Html.DisplayFor(model => model[i].Ogrenci.Ad)
}
我有两个模型。我如何从视图访问 FK 值或我应该如何去做? HTML 页面没有任何内容。
模型 1:
public class Ogrenci
{
public int OgrenciID { get; set; }
public int OgrenciNumarasi { get; set; }
public string Ad { get; set; }
public string Soyad { get; set; }
public string Bolum { get; set; }
public short Sinif { get; set; }
public string Yetenekler { get; set; }
public string Sifre { get; set; }
public List<Proje> Projeler { get; set; }
}
模型 2:
public class Proje
{
public int ProjeID { get; set; }
public string ProjeAdi { get; set; }
public string Aciklama { get; set; }
public DateTime EklenmeTarihi { get; set; }
public int OgrenciID { get; set; }
public Ogrenci Ogrenci { get; set; }
}
控制器:
public ActionResult Index()
{
return View(db.Projeler.ToList());
}
查看:
@model IEnumerable<DonemProjesi.Models.Proje>
@{
ViewBag.Title = "Index";
}
@Html.DisplayFor(modelItem => item.Ogrenci.Ad) //the problem is here
尝试指定要包含在查询结果中的相关对象:
return View(db.Projeler.Include(o => o.Ogrenci).ToList());
试试这个
public ActionResult Index()
{
return View(db.Ogrenci.Include(o => o.Projeler).FirstOrDefault());
}
@model DonemProjesi.Models.Ogrenci
@{
ViewBag.Title = "Index";
}
@Html.DisplayFor(m => m.Ad)
@for (int i = 0; i < Model.Projeler.Count; i+=1)
{
@Html.DisplayFor(model =>model.Projeler[i].ProjeAdi)
}
或者你是这个意思
public ActionResult Index()
{
return View(db.Projeler.Include(o => o.Ogrenci).ToList());
}
@model IEnumerable<DonemProjesi.Models.Proje>
@{
ViewBag.Title = "Index";
}
@for (int i = 0; i < Model.Count; i+=1)
{
@Html.DisplayFor(model =>model[i].ProjeAdi)
@Html.DisplayFor(model => model[i].Ogrenci.Ad)
}