Answer the question
In order to leave comments, you need to log in
Can't display data from related tables in View?
Hello! There are two related tables based on SQL Server.
1 - "Kolledjs": ID - primary key, RUNAME,RUADRES....
2nd table "Spess": ID-primary key, KolledjId (for foreign key of Kolledjs table), RUSPES...
I want to display in view from table , but nothing comes out, a blank page
When choosing a college, the specialty of the college should be displayed
Actually the model itself:
namespace Abit.Models
{
public class Spes
{
public int Id { get; set; }
public int? KolledjId { get; set; }
public Kolledj Kolledj { get; set; }
public string RUSPES { get; set; }
public string RUKVAL { get; set; }
}
public class Kolledj
{
public int ID { get; set; }
public string RUNAME { get; set; }
public string RUADRES { get; set; }
public string KAT { get; set; }
public int RAITING { get; set; }
public string TELEFONSITE { get; set; }
public ICollection<Spes> Spess { get; set; }
public Kolledj()
{
Spess = new List<Spes>();
}
}
}
namespace Abit.Models
{
public class KContext:DbContext
{
public DbSet<Spes> Spess { get; set; }
public DbSet<Kolledj> Kolledjs { get; set; }
}
}
public class HomeController : Controller
{
KContext db = new KContext();
public ActionResult Index()
{
var sp = db.Spess.Include(s => s.Kolledj);
return View(sp.ToList());
}
}
public ActionResult Details(int? id)
{
var sp= db.Spess.Include(s => s.Kolledj).FirstOrDefault(s => s.Id == id);
if (sp== null)
{
return HttpNotFound();
}
return View(sp);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question