ASP.NET MVC one to one associated data is not displaying on index page -
my case product has 1 stock. have added productid on stocks table. table has records this:
products table:
id | name | description | sellingprice
------------------------------------------------
1 | product1 | desc | 10.20
2 | product2 | prod desc | 20.55
------------------------------------------------
stocks table:
id | productid | qty
------------------------------------------------
1 | 1 | 100
2 | 2 | 250
------------------------------------------------
now on products index page want display data on html table this:
<table border="1"> <tr> <th>name</th> <th>description</th> <th>selling price</th> <th>stock</th> </tr> <tr> <td>product1</td> <td>desc</td> <td>10.20</td> <td>100 (note: cannot see stock data when open products index, want see)</td> </tr> <tr> <td>product2</td> <td>product desc</td> <td>20.55</td> <td>250 (note: cannot see stock data when open products index, want see)</td> </tr> </table>
below mvc code have done till now:
public class product { public int id { get; set; } [required] public string name { get; set; } [required] public string description { get; set; } public decimal sellingprice { get; set; } public virtual stock stock { get; set; } } public class stock { public int id { get; set; } public int qty { get; set; } public int productid { get; set; } [required] public virtual product product { get; set; } } // get: products public actionresult index() { var products = db.products.tolist(); return view(products); } <table class="table"> <tr> <th> @html.displaynamefor(model => model.name) </th> <th> @html.displaynamefor(model => model.description) </th> <th> @html.displaynamefor(model => model.sellingprice) </th> <th>@html.displaynamefor(model => model.stock)</th> <th></th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.name) </td> <td> @html.displayfor(modelitem => item.description) </td> <td> @html.displayfor(modelitem => item.sellingprice) </td> <td> @html.displayfor(modelitem => item.stock.qty) </td> <td> @html.actionlink("edit", "edit", new { id = item.id }) | @html.actionlink("details", "details", new { id = item.id }) | @html.actionlink("delete", "delete", new { id = item.id }) </td> </tr> }
@html.displayfor(modelitem => item.stock.qty particular piece of code not returning record, why cannot see stock data on product index page. appreciated, thank you!
here attachment, screenshot of generated table in sql
you need add on query:
//don't forget add on using statements using system.data.entity; var products = db.products.include(x=>x.stock).tolist();
you need include extension methods first level relationship querying table.
update model well, include id of stock model:
public class product { public int id { get; set; } [required] public string name { get; set; } [required] public string description { get; set; } public decimal sellingprice { get; set; } public virtual int stockid {get;set;} //add public virtual stock stock { get; set; } }
don't forget update database model.
Comments
Post a Comment