如何从ViewModel中读取数据

时间:2016-01-07 14:54:17

标签: c# asp.net-mvc entity-framework linq asp.net-mvc-5

我想获得Unit_Price和Quantity to ActionResult Index。

任何帮助表示赞赏

模型产品

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; }

}

控制器产品

[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);

查看产品索引

@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>

我在代码中尝试了很多方法 也许某些东西会有用。

enter image description here

2 个答案:

答案 0 :(得分:1)

Product在其定义中有一个集合ProductsDetails

您正在循环浏览产品系列

@foreach (var _product in Model.Produkty)

但在此循环中,您可以执行以下操作

@foreach (var _cena in Model.ProductyDetails)

老实说没有多大意义,因为它只会显示所有产品细节。

相反,您应该只在ProductDetails对象中显示_product

@foreach (var _cena in _product.ProductsDetails)

至少我认为这就是正在发生的事情。

答案 1 :(得分:0)

我注意到的第一个问题是,我信任的是类型-o的副本,是查看产品索引的第1行。 它应该是:

@model CRM_Hurtownia.ViewModels.ProductViewModel

@model CRM_Hurtownia.ViewModels.ProduktViewModel

我注意到的另一个问题是,在查看产品索引第15,18和21行中,您希望在特定属性上使用 DisplayNameFor 。你必须为他们定义一个名字...... 因此,进入产品视图模型并在每个属性上方添加以下内容:

[Display(Name = "Set Primary")]

看起来应该是这样的:

public class ProductViewModel
{
   [Display(Name = "Name for Categories")]
   public IEnumerable<Category> Categories { get; set; }
   [Display(Name = "Name for Subcategories ")]
   public IEnumerable<Subcategory> Subcategories { get; set; }
   [Display(Name = "Name for Producty ")]
   public IEnumerable<Product> Producty { get; set; }
   [Display(Name = "Name for ProductyDetails ")]
   public IEnumerable<ProductDetails> ProductyDetails { get; set; }
}

另一个问题是你的查看产品索引中的foreach声明。 VM中没有 Produkty 的属性,它应该是 Producty

@foreach (var _product in Model.Producty)

如果是,现在我感到困惑,因为看起来 Producty 正在使用 Model Product Product class 中的属性。如果是,那么你需要

<强> 1)

型号产品中的类从产品更改为 Producty 。将第1行和第3行更改为:

public class Producty
{
    public Producty()
    {

<强> 2) 或者您可以将产品索引视图中的foreach更改为:

@foreach (var _product in Model.Product)

并将产品视图模型更改为:

public class ProductViewModel
{
   [Display(Name = "Name for Categories")]
   public IEnumerable<Category> Categories { get; set; }
   [Display(Name = "Name for Subcategories ")]
   public IEnumerable<Subcategory> Subcategories { get; set; }
   [Display(Name = "Name for Producty ")]
   public IEnumerable<Product> Product { get; set; }
   [Display(Name = "Name for ProductyDetails ")]
   public IEnumerable<ProductDetails> ProductyDetails { get; set; }
}

如果您以这种方式进行更改,那么在 Controller Product 的第9行,您必须将其更改为:

var _product = db.Product .Include(x => x.Categories)

再次,在第17行 Product Controller 上,它引用了 ProductyDetails 类。这个类不存在。我猜你应该把它作为 ProductDetails 吗?将其更改为:

var _price = db.Producty    .Join(db.ProductDetails,

同时检查第31和38行的语法。

我可能会选择第一个选项:

  

型号产品中的类从产品更改为 Producty 。   减少工作量。

检查你的语法。我不确定您是否还有其他逻辑错误。