【发布时间】:2020-05-17 19:59:29
【问题描述】:
有人可以帮我知道为什么model.Prod 如下图所示为空,而model.catg 不为空吗?我的产品表中有项目。而且在我将@Html.HiddenFor(p=>p.Prod[i].id) 添加到视图之后,model.Prod 不为空,但只有id 具有价值。其他参数仍然为空。
@model HandMShop.ViewModel.HMViewM
@using (Html.BeginForm("ShopPage", "Shop", FormMethod.Post)) {
@for(int i=0;i< Model.catg.Count;i++) {
@Html.CheckBoxFor(model => model.catg[i].IsChecked)
<label>@Model.catg[i].CategoryName</label>
@Html.HiddenFor(model => model.catg[i].id)
@Html.HiddenFor(model => model.catg[i].CategoryName }
@for (int i = 0; i < Model.Prod.Count; i++) {
<div class="col-md-6 col-lg-4">
<figure class="card card-product mehsul">
<div class="img-wrap"> <img class="img-fluid mehsulimg"
src="@Model.Prod[i].PhotoProducts.First().ImageName" alt=""> </div>
<div class="handhover">
<img class="img-fluid" src="@Model.Prod[i].PhotoProducts.Last().ImageName" alt="">
</div>
<figcaption class="info-wrap">
<h4 class="title">@Model.Prod[i].ProdName</h4>
<p class="desc">@Model.Prod[i].Description</p>
</figcaption>
</figure>
</div>
}
查看我使用的模型:
public class HMViewM
{
public List<User> userr { get; set; }
public List<homesec1slider> homesec1 { get; set; }
public List<Category> catg { get;set; }
public List<Colour> colrs { get; set; }
public List<PhotoProduct> Photopr { get; set; }
public List<Product> Prod { get; set; }
public bool Istehsal { get; set; }
}
控制器
[HttpPost]
public ActionResult ShopPage(HMViewM model)
{
List<Category> selectedcatg = model.catg.Where(x => x.IsChecked == true).ToList();
List<Product> selectedprod = model.Prod.Where(i => i.CategoryId == selectedcatg.FirstOrDefault().id).ToList();
var vm = new HMViewM
{
Prod = selectedprod,
catg = _context.Categories.ToList(),
colrs = _context.Colours.ToList(),
Photopr = _context.PhotoProducts.Where(ph => ph.Product.id == ph.ImageId).ToList(),
};
return View(vm);
}
【问题讨论】:
标签: c# asp.net asp.net-mvc entity-framework