c# - ForeignKey navigation property value not showing up in Details and Delete Views -


i have 2 models project , category each project contains multiple categories.

project.cs

public class project {     public int id { get; set; }      [required]     [display(name="project name")]     public string name { get; set; }      public icollection<category> categories { get; set; } } 

category.cs

public class category {     public int id { get; set; }      [required]     [display(name="category name")]     public string name { get; set; }      [required]     [foreignkey("project")]     public int projectid { get; set; }      public project project { get; set; } } 

after scaffolding when click on details or delete (after adding projects , categories) link category, project name not show in both views! label shows up. shows in index , edit views though. reason that?

edit

this details action

    public actionresult details(int? id)     {         if (id == null)         {             return new httpstatuscoderesult(httpstatuscode.badrequest);         }         category category = db.categories.find(id);         if (category == null)         {             return httpnotfound();         }         return view(category);     } 

when added line var pro = category.project before returning view(category) , started debugging, pro variable set null. in index action project explicitly included db.categories.include(c => c.project).tolist() not in details. need include explicitly? if yes, how?

you can set navigation virtual(lazy loading)

public virtual project project { get;set; } 

or can use eager loading using include method(no need virtual keyword)

category category = db.categories.include(c => c.project).firstordefault(c => c.id == id); 

msdn documentation


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -