Click here to Skip to main content
14,699,126 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hello,
I would like to get Unit_Price and Quantity to ActionResult Index.

any help appreciated

Model Product

[Table("Product")]
public class Product
{
    public Product()
    {
        ProductyDetails = 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> ProductyDetails { 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 Product

public class ProductViewModel
{
    public IEnumerable<Category> Categories { get; set; }
    public IEnumerable<Subcategory> Subcategories { get; set; }
    public IEnumerable<Product> Products { get; set; }
    public IEnumerable<ProductDetails> ProductsDetails { get; set; }

}


Controller Product

[Authorize]
public class ProductController : Controller
{
    // GET: Product
    private Context db = new Context();
    public ActionResult Index()
    {

        var _product = db.Product .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,
            Products = _product,
            //ProductsDetails = _price
            //ProductsDetails =_productDetails

        };

        return View(vm);


View Product Index

@model CRM_Hurtownia.ViewModels.ProductViewModel

@{
    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.Producty)
        </th>
        <th>
            cena
        </th>
        <th>
            Quantity
        </th>
        <th></th>
    </tr>

    @foreach (var _Product in Model.Producty)
    {

        <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_Product }) |
                @Html.ActionLink("Details", "Details", new { id = _Product.ID_Product }) |
                @Html.ActionLink("Delete", "Delete", new { id = _Product.ID_Product })
            </td>
        </tr>

    }
</table>


I have tried many ways hence these comments in the code
maybe something of that will be useful.
Posted
Updated 7-Jan-16 3:51am
v4

1 solution

If you want to bind to a collection you can't use "foreach", you have to use "for" and access the elements via an index

razor - ASP.NET MVC 4 - for loop posts model collection properties but foreach does not - Stack Overflow[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900