2013-03-31 35 views
0

我想實現一個多對多的上下文。 A產品可以由不同的材料製成,並具有不同的價格對於那些材料:.net mvc許多對許多要發佈的內容

這裏是product.cs:

namespace test.Models 
{ 
    using System.ComponentModel; 
    using System.ComponentModel.DataAnnotations; 
    using System.Web.Mvc; 
    using System; 
    using System.Collections.Generic; 
    public partial class Product 
    { 
     public Product() 
     { 
      this.ProductMaterials = new HashSet<ProductMaterial>(); 
     } 
     public int Id { get; set; } 
     [DisplayName("Product name"), 
      Required(ErrorMessage = "Product Name is required"), 
      StringLength(100)] 
     public string Name { get; set; } 
     [Required] 
     public virtual ICollection<ProductMaterial> ProductMaterials { get; set; } 
    } 
} 

這裏是ProductMaterials.cs:

namespace test.Models 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class ProductMaterial 
    { 
     public int Id { get; set; } 
     public int ProductId { get; set; } 
     public int MaterialId { get; set; } 
     public string PriceOffset { get; set; } 

     public virtual Material Material { get; set; } 
     public virtual Product Product { get; set; } 
    } 
} 

而且Material.cs:

namespace test.Models 
{ 
    using System.ComponentModel; 
    using System.ComponentModel.DataAnnotations; 
    using System.Web.Mvc; 
    using System; 
    using System.Collections.Generic; 

    public partial class Material 
    { 
     public Material() 
     { 
      this.ProductMaterials = new HashSet<ProductMaterial>(); 
     } 

     public int Id { get; set; } 
     [Required,DisplayName("Material"), 
      StringLength(100)] 
     public string Mat { get; set; } 
     [DefaultValue(0), Required] 
     public decimal PriceOffset { get; set; } 

     public virtual ICollection<ProductMaterial> ProductMaterials { get; set; } 
    } 
} 

現在試圖在上下文中添加正確的信息,因此obj ect可以從表單創建,試圖儘可能簡單地通過小步驟來理解這一點。

的testContext.cs具有下列:利用

System.Data.Entity的;

namespace test.Models 
{ 
    public class testContext : DbContext 
    { 
System.Data.Entity.DropCreateDatabaseIfModelChanges<test.Models.testContext>()); 
     public testContext() : base("name=testContext") 
     { 
     } 
     public DbSet<Product> Products { get; set; } 
     public DbSet<Manufacturer> Manufacturers { get; set; } 
     public DbSet<ProductMaterial> ProductMaterials { get; set; } 
     public DbSet<Material> Materials { get; set; } 
     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
     modelBuilder.Entity<Product>() 
      .HasMany(c => c.ProductMaterials); 
     }  
    } 
} 

這是額外的數據我POST:

<input type="text" value="1" name="ProductId"> 
<input type="text" value="1" name="MaterialId"> 
<input type="text" value="0" name="PriceOffset"> 

當我設置控制器我看到一個產品對象與0 ProductMaterials對象創建一個斷點。

不知道這是否是正確的方式來做到這一點,我看到在我下載的示例代碼中,更新將更加複雜。

+0

究竟是什麼問題?你需要在HTML中放置什麼?我們無法在此爲您設計一個完整的用戶界面! – greg84

+0

不,只是要輸入到創建控制器的POST – HMR

+0

什麼是創建操作應該做的?用一種材料創建新產品?或者有很多材料?或者爲現有產品和材料創建新的ProductMaterial關係? – Slauma

回答

0

放棄如何使用添加的材料創建新產品。知道它爲編輯產品工作。

加在的global.asax.cs路線:改變了指數

routes.MapRoute(
     "Materials For Product", // Route name 
     "Materials/{action}/{ProductId}/{MaterialId}", // URL with parameters 
     new { controller = "ProductMaterial", action = "Index", 
       ProductId = UrlParameter.Optional, 
       MaterialId = UrlParameter.Optional 
     } // Parameter defaults 
    ); 

,創建並在ProductMaterialController.cs刪除產品的

public ViewResult Index(int ProductId) 
{ 
    var productmaterialss = db.ProductMaterials.Include(p => p.Material).Include(p => p.Product).Where(
     p => p.ProductId == ProductId); 
    Product pr = db.Products.Find(ProductId); 
    ViewData["ProductId"] = ProductId; 
    ViewData["ProductName"] = pr.Name; 
    return View(productcolors.ToList()); 
} 
public ActionResult Create(int ProductId) 
{ 
    Product p = db.Products.Where(i => i.Id == ProductId).Single(); 
    ViewBag.MaterialId = new SelectList(db.Materials, "Id", "Mat"); 
    ViewData["ProductName"] = p.Name; 
    ViewData["ProductId"] = p.Id; 
    return View(); 
} 
[HttpPost] 
public ActionResult Create(ProductMaterial productmaterial) 
{ 
    if (ModelState.IsValid) 
    { 
     db.ProductMaterials.Add(productmaterial); 
     db.SaveChanges(); 
     return RedirectToAction("Index/"+productmaterial.ProductId); 
    } 
    Product p = db.Products.Where(i => i.Id == productmaterial.ProductId).Single(); 
    ViewBag.MaterialId = new SelectList(db.Materials, "Id", "Mat"); 
    ViewData["ProductName"] = p.Name; 
    ViewData["ProductId"] = p.Id; 
    return View(); 
} 
[HttpPost, ActionName("Delete")] 
public ActionResult DeleteConfirmed(int id) 
{ 
    ProductMaterial productmaterial = db.ProductMaterials.Find(id); 
    int ProductId = productcolor.ProductId; 
    db.ProductMaterials.Remove(productmaterial); 
    db.SaveChanges(); 
    return RedirectToAction("Index/"+ProductId); 
} 

更改編輯視圖中打開一個模型對話框使用ProductMaterial視圖並更改所有ProductMaterial視圖(僅索引和刪除,因爲其他未使用)不包含模板並顯示產品名稱。

向產品添加腳本以打開所有鏈接和窗體上的Modal和附加事件偵聽器到Ajax提交所需信息並重置模態對話框的innerHTML與響應。