U
U
User992018-03-22 13:24:32
ASP.NET
User99, 2018-03-22 13:24:32

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
5ab3832d0b6c4262663036.jpeg
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>();
        }
    }
}

Context:
namespace Abit.Models
{
    public class KContext:DbContext
    {
        public DbSet<Spes> Spess { get; set; }
        public DbSet<Kolledj> Kolledjs { get; set; }
    }
}

Controller
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);
}

Where am I doing wrong?
And how can it be done differently? For example, as in the picture, selects a college, and next to it in the table, the specialty of the selected college is loaded

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question