, поэтому я работаю над этим проектом, но я застрял на том, "как выбрать несколько продуктов из выпадающего списка" и добавить их в пакет ... вот сущность продукта
public enum avai { In_Stock , Out_Of_Stok}
public class Product
{
[Key]
public int IdProduct { get; set; }
public String NameProduct { get; set; }
public String Description { get; set; }
public float Price { get; set; }
public String ImageProduct { get; set; }
public avai Availability { get; set; }
public int? IdCategory { get; set; }
public int? IdDiscount { get; set; }
public int? IdStock { get; set; }
[ForeignKey ("IdCategory")]
public virtual Category Category { get; set; }
public int? IdAd { get; set; }
public int? IdPack { get; set; }
[ForeignKey("IdAd")]
public virtual Ads Ad { get; set; }
[ForeignKey("IdPack")]
public virtual Pack Pack { get; set; }
[ForeignKey("IdDiscount")]
public virtual Discount Discount { get; set; }
[ForeignKey("IdStock")]
public virtual Stock Stock { get; set; }
}
этоСущность пакета
public class Pack
{
[Key]
public int IdPack { get; set; }
public int? IdAd { get; set; }
[ForeignKey("IdAd")]
public virtual Ads Ad { get; set; }
public Boolean Availability { get; set; }
public int? IdProduct { get; set; }
[ForeignKey("IdProduct")]
public virtual Product Product { get; set; }
public string PackName { get; set; }
public virtual ICollection<Product> ListProducts { get; set; }
}
это конфигурация пакета
public class PackConfiguration:EntityTypeConfiguration<Pack>
{
public PackConfiguration()
{
HasMany(pack => pack.ListProducts).WithRequired(prod => prod.Pack).HasForeignKey(pack =>
pack.IdProduct);
}
}
это конфигурация продукта
public class ProductConfiguration:EntityTypeConfiguration<Product>
{
public ProductConfiguration() {
HasRequired(Prod => Prod.Category)
.WithMany(cat => cat.ListProducts)
.HasForeignKey(prod => prod.IdCategory)
.WillCascadeOnDelete(true);
HasOptional(Prod => Prod.Pack)
.WithMany(pack => pack.ListProducts)
.HasForeignKey(prod => prod.IdPack)
.WillCascadeOnDelete(true);
HasOptional(Prod => Prod.Discount)
.WithMany(disc => disc.ListProducts)
.HasForeignKey(prod => prod.IdDiscount)
.WillCascadeOnDelete(true);
HasRequired(Prod => Prod.Stock)
.WithMany(st => st.ListProducts)
.HasForeignKey(prod => prod.IdStock)
.WillCascadeOnDelete(true);
}
}
это контроллер пакета
using PiDevCRM.Domain.Entities;
using PiDevCRM.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace PiDevCRM.Web.Controllers
{
public class PacksController : Controller
{
public PackService PaS;
public ProductService ps;
public PacksController()
{
PaS = new PackService();
ps = new ProductService();
}
// GET: Packs
public ActionResult Index()
{
return View(PaS.GetAll());
}
// GET: Packs/Details/5
public ActionResult Details(int id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Pack pack = PaS.GetById(id);
if (pack == null)
{
return HttpNotFound();
}
return View(pack);
}
// GET: Packs/Create
public ActionResult Create()
{
var ProductList = ps.GetAll();
ViewBag.Products = new SelectList(ProductList, "IdProduct", "NameProduct");
return View();
}
// POST: Packs/Create
[HttpPost]
public ActionResult Create(Pack pack)
{
try
{
PaS.Add(pack);
PaS.Commit();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Packs/Edit/5
public ActionResult Edit(int id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Pack pack = PaS.GetById(id);
if (pack == null)
{
return HttpNotFound();
}
return View(pack);
}
// POST: Packs/Edit/5
[HttpPost]
public ActionResult Edit(int id,Pack p)
{
try
{
p = PaS.GetById(id);
PaS.Update(p);
PaS.Commit();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Packs/Delete/5
public ActionResult Delete(int id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Pack pack = PaS.GetById(id);
if (pack == null)
{
return HttpNotFound();
}
return View(pack);
}
// POST: Packs/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult ConfirmDelete(int id)
{
Pack pack = PaS.GetById(id);
PaS.Delete(pack);
PaS.Commit();
return RedirectToAction("Index");
}
}
это пакетное представление
@model PiDevCRM.Domain.Entities.Pack
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Pack</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PackName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PackName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PackName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ListProducts, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ListBoxFor(model => model.IdProduct, ViewBag.Products as SelectList, "choose..")
@Html.ValidationMessageFor(model => model.IdProduct, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
я бы соврал, что у меня есть экранное представление примерно такого типа
"PackName: -------------
ListProduct: ----------- ----------- ----------- "
Я попробовал мульти- выбрав выпадающий список и выберите 2 или 3 продукта, но я получаю эту ошибку «IdProduct должен быть числом»
спасибо за помощь заранее