我刚刚开始使用MVC,非常感谢帮助解决问题。我有一组制造商,我正在为特定制造商选择一组产品,并采取其中一些。之后,我再为另一家制造商重复相同的程序,依此类推。现在的问题。我如何在一个列表(不在表中)中累积所选产品以供将来处理。
答案 0 :(得分:0)
您的产品型号可能如下:
public int ProductId { set; get; }
public string Code { set; get; }
public string Name { set; get; }
public int ManufacturerId { set; get; }
public virtual Manufacrer Manufacrer { set; get; }
您的产品控制器可以执行以下操作:(尽管在视图中使用了显示按钮,但不使用jquery)
public ViewResult ViewProductStatus(int? manufacturerId)
{
ViewBag.ManufacturerId = new SelectList(db.Manufacturers, "ManufacturerId", "Code");
if (Manufacturer != null)
{
return View(db.Products.Include(c => c.Manufacturer).Where(c => c.Manufacturers.ManufacturerId == manufacturerId));
}
else
{
return View(db.Products.Include(c => c.Manufacturers));
}
}
在“产品视图”下创建一个视图,将其命名为ViewProduct,您的ViewProduct可以是这样的:
@model IEnumerable<Appname.Models.Product>
@{
ViewBag.Title = "View Product";
}
<h2>View Product</h2>
@using (Html.BeginForm())
{
@Html.DropDownList("manufacturerId", String.Empty)
<input type="submit" value="Show" />
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<div>
@Html.DisplayNameFor(model => model.Code)
</div>
<div>
@Html.DisplayNameFor(model => model.Name)
</div>
@foreach (var item in Model)
{
<div>
@Html.DisplayFor(modelItem => item.Code)
</div>
<div>
@Html.DisplayFor(modelItem => item.Name)
</div>
</div>
}