ViewModel的问题 [英] The problem with ViewModel
本文介绍了ViewModel的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我想获得Unit_Price和Quantity to ActionResult Index。
任何帮助赞赏
型号产品
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产品
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.
推荐答案
如果你想绑定到一个集合,你不能使用foreach,你必须使用for并通过索引访问元素
razor - ASP.NET MVC 4 - for循环发布模型集合属性但foreach没有 - Stack Overflow [ ^ ]
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[^]
这篇关于ViewModel的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文