c# - MVC4 Foreign Key property is null -


i have expenses , categories tables. have seeded tables few entries, when pulled db category property null. expect automatically populated entries category table. missing?

model:

public class expense {     public int expenseid { get; set; }      [datatype(datatype.currency)]     public decimal amount { get; set; }      [foreignkey("categoryid")]     public category category { get; set; }      [required(errormessage = "you must select category!")]     public int categoryid { get; set; } } public class category {     public int categoryid { get; set; }     public string description { get; set; }     public virtual icollection<expense> expenses { get; set; } } 

view:

@foreach (var item in model) {     <tr>         <td>             @html.displayfor(modelitem => item.amount)         </td>         <td>             @item.categoryid         </td>         <td>             @item.category.description //nullreferenceexception here         </td>     </tr> 

controller:

// get: expenses     public actionresult index()     {         return view(db.expenses.tolist());     } 

try marking category property virtual. navigation properties need virtual support lazy loading if not eager loaded.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -