c# - how to read data from the ViewModel -


i unit_price , quantity actionresult index.

any appreciated

model product

public class product {     public product()     {         productsdetails = new hashset<productdetails>;();     }     [key]//[required]     public int id_product { get; set; }     [required]     public int id_subcategory { get; set; }     [required]     public int id_category { get; set; }     // [stringlength(50)]     public string product_name { get; set; }     //public byte photo_products { get; set; }     // id_subcategory      //public virtual productdetails productdetails { get; set; }     [foreignkey("id_subcategory")]     public subcategory subcategories { get; set; }      [foreignkey("id_category")]     public virtual category categories { get; set; }     public virtual icollection<productdetails> productsdetails { get; set; }     public virtual icollection<orderdetails> ordersdetails { get; set; }  } 

model productdetails

[table("product_details")] public class productdetails {     public productdetails()     {      }     [key]     public int id { get; set; }     //[foreignkey("product")]     public int id_product { get; set; }     [column(typename ="money")]     public decimal unit_price{ get; set; }     public int quantity { get; set; }     public float vat { get; set; }     public string description { get; set; }     [column(typename = "money")]     public decimal gross_value { get; set; }     [column(typename = "money")]     public decimal net_value { get; set; }     // id_product     public virtual product product { get; set; } } 

viewmodel producty

public class productviewmodel {     public ienumerable<category> categories { get; set; }     public ienumerable<subcategory> subcategories { get; set; }     public ienumerable<product> producty { get; set; }     public ienumerable<productdetails> productydetails { get; set; }  } 

controller product

[authorize] public class productcontroller : controller {     // get: product     private context db = new context();     public actionresult index()     {          var _product = db.producty .include(x => x.categories)                                    .include(x => x.subcategories)                                    .orderby(x => x.categories.name_category)                                    .tolist();          var _category = db.categories.tolist();         var _subcategory = db.subcategories.tolist();          var _price = db.producty    .join(db.productydetails,                                     sc => sc.id_product,                                     soc => soc.id_product,                                     (sc, soc) => new                                     {                                         product = sc,                                         productdetails = soc                                     }).select(soc => new {                                         cena = soc.productdetails.unit_price,                                        quantity = soc.productdetails.quantity                                     }).tolist();           var _productdetails = db.productydetails.include(x => x.unit_price)                                                     .select(x => x.unit_price)                                                     .tolist();         var vm = new productviewmodel()         {             //categories = _category,             //subcategories = _subcategory,             producty = _product,             //productydetails = _price             //productydetails =_productdetails          };          return view(vm); 

view product index

@model crm_hurtownia.viewmodels.produktviewmodel  @{     viewbag.title = "product";     viewbag.active = "product";     layout = "~/views/shared/_layout.cshtml"; }  <h4 class="page-title">product</h4>  @html.actionlink("create product", "create", null, new { @class = "btn m-r-5" })  <table class="table table-bordered table-hover tile">     <tr>         <th>             @html.displaynamefor(model => model.categories)         </th>         <th>             @html.displaynamefor(model => model.subcategories)         </th>         <th>             @html.displaynamefor(model => model.products)         </th>         <th>             price         </th>         <th>             quantity         </th>          <th></th>     </tr>      @foreach (var _product in model.produkty)     {          <tr>             <td>                 @html.displayfor(modelitem => _product.categories.name_category)             </td>             <td>                 @html.displayfor(modelitem => _product.subcategories.name_subcategory)             </td>             <td>                 @html.displayfor(modelitem => _product.product_name)             </td>             @*@foreach (var _cena in model.productydetails)             {             <td>                 @html.displayfor(modelitem => _cena.unit_price)             </td>             <td>                 @html.displayfor(modelitem => _cena.quantity)             </td>              }*@               <td>                 @html.actionlink("edit", "edit", new { id = _product.id_produkt }) |                 @html.actionlink("details", "details", new { id = _product.id_produkt }) |                 @html.actionlink("delete", "delete", new { id = _product.id_produkt })             </td>         </tr>      } </table> 

i have tried many ways hence these comments in code maybe of useful.

enter image description here

product has collection productsdetails in it's definition.

you looping through products collection here

@foreach (var _product in model.produkty) 

but within loop following

@foreach (var _cena in model.productydetails) 

which honest doesn't make sense, since show product details.

instead, should show productdetails in _product object:

@foreach (var _cena in _product.productsdetails) 

at least think happening.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -