2015-12-02 51 views
1

保存我的產品時遇到了一些問題。 Dropdownlist正在填充,當我嘗試保存時出現,我有一個標題中指出的錯誤。我讀了很多關於它的文章,但其中的任何一篇都幫我解決了。什麼會影響我的代碼?有任何想法嗎?'CategoryID'鍵的類型是'System.Int32',但必須是'IEnumerable <SelectListItem>'的類型'

控制器:

// GET: Admin/Products/Create 
public ActionResult Create() 
{ 
    string domain = Request.Url.Host; 
    int clientid = (from a in db.Client where a.Domain == domain select a.ID).First(); 

    int maxID = db.Product.Where(c => c.ClientID == clientid).Max(c => (int?)c.ProductID) ?? 0; 
    ViewBag.MaxID = maxID + 1; 

    List<SelectListItem> categories = new List<SelectListItem>(); 
    foreach (var cat in db.Category.Where(c => c.ClientID == clientid)) 
    { 
     categories.Add(new SelectListItem() { Text = cat.Name, Value = cat.CategoryID.ToString() }); 
    } 

    ViewBag.Categories = categories; 

     return View(); 
} 

// POST: Admin/Products/Create 
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "ID,ProductID,Name,CategoryID,Price,Promotion,Image1,Image2,Image3,Image4,Description,ClientID")] Product product, HttpPostedFileBase file1, HttpPostedFileBase file2, HttpPostedFileBase file3, HttpPostedFileBase file4) 
{ 
    if (ModelState.IsValid) 
    { 
     if (file1 != null) 
     { 
      var fileName1 = Path.GetFileName(file1.FileName); 
      var path1 = Path.Combine(Server.MapPath("~/Images/Products/"), fileName1); 
      file1.SaveAs(path1); 

      @product.Image1 = Url.Content("~/Images/Products/" + fileName1); 
     } 

     if (file2 != null) 
     { 
      var fileName2 = Path.GetFileName(file2.FileName); 
      var path2 = Path.Combine(Server.MapPath("~/Images/Products/"), fileName2); 
      file2.SaveAs(path2); 

      @product.Image2 = Url.Content("~/Images/Products/" + fileName2); 
     } 

     if (file3 != null) 
     { 
      var fileName3 = Path.GetFileName(file3.FileName); 
      var path3 = Path.Combine(Server.MapPath("~/Images/Products/"), fileName3); 
      file3.SaveAs(path3); 

      @product.Image3 = Url.Content("~/Images/Products/" + fileName3); 
     } 

     if (file4 != null) 
     { 
      var fileName4 = Path.GetFileName(file4.FileName); 
      var path4 = Path.Combine(Server.MapPath("~/Images/Products/"), fileName4); 
      file4.SaveAs(path4); 

      @product.Image4 = Url.Content("~/Images/Products/" + fileName4); 
     } 

     string domain = Request.Url.Host; 
     int clientid = (from a in db.Client where a.Domain == domain select a.ID).First(); 

     List<SelectListItem> categories = new List<SelectListItem>(); 
     foreach (var cat in db.Category.Where(c => c.ClientID == clientid)) 
     { 
      categories.Add(new SelectListItem() { Text = cat.Name, Value = cat.CategoryID.ToString() }); 
     } 

     ViewBag.Categories = categories; 

     db.Product.Add(product); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    return View(product); 
} 

DROPDOWNLIST在查看:

<div class="col-md-2"> 
    <div class="form-group"> 
     <label>Kategoria</label> 
     @Html.DropDownListFor(model => model.CategoryID, (IEnumerable<SelectListItem>)ViewBag.Categories, new { @class = "form-control" }) 
     @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) 
    </div> 
</div> 

型號:

public partial class Product 
    { 
     public int ID { get; set; } 
     public int ProductID { get; set; } 
     public string Name { get; set; } 
     public int CategoryID { get; set; } 
     public decimal Price { get; set; } 
     public int Promotion { get; set; } 
     public string Image1 { get; set; } 
     public string Image2 { get; set; } 
     public string Image3 { get; set; } 
     public string Image4 { get; set; } 
     [DataType(DataType.MultilineText)] 
     [AllowHtml] 
     public string Description { get; set; } 
     public int ClientID { get; set; } 
    } 
+0

先這樣線程被建議作爲相關似乎解決您的具體問題:http://stackoverflow.com/a/8596202/728795 – Andrei

+0

是的,我發現了一個又一個太.. HTTP://計算器。 com/questions/27346367/the-viewdata-item-that-has-the-key-categoryid-is-of-type-system-int32-but-mu – JamieD77

+0

它都不能解決我的問題......不幸的是。 – Ashiv3r

回答

1

你沒有設置ViewBag.Categories在您的文章,如果ModelState中是無效的。

移動你的選擇列表中的代碼塊的IsValid

// POST: Admin/Products/Create 
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "ID,ProductID,Name,CategoryID,Price,Promotion,Image1,Image2,Image3,Image4,Description,ClientID")] Product product, HttpPostedFileBase file1, HttpPostedFileBase file2, HttpPostedFileBase file3, HttpPostedFileBase file4) 
{ 
    if (ModelState.IsValid) 
    { 
     if (file1 != null) 
     { 
      var fileName1 = Path.GetFileName(file1.FileName); 
      var path1 = Path.Combine(Server.MapPath("~/Images/Products/"), fileName1); 
      file1.SaveAs(path1); 

      @product.Image1 = Url.Content("~/Images/Products/" + fileName1); 
     } 

     if (file2 != null) 
     { 
      var fileName2 = Path.GetFileName(file2.FileName); 
      var path2 = Path.Combine(Server.MapPath("~/Images/Products/"), fileName2); 
      file2.SaveAs(path2); 

      @product.Image2 = Url.Content("~/Images/Products/" + fileName2); 
     } 

     if (file3 != null) 
     { 
      var fileName3 = Path.GetFileName(file3.FileName); 
      var path3 = Path.Combine(Server.MapPath("~/Images/Products/"), fileName3); 
      file3.SaveAs(path3); 

      @product.Image3 = Url.Content("~/Images/Products/" + fileName3); 
     } 

     if (file4 != null) 
     { 
      var fileName4 = Path.GetFileName(file4.FileName); 
      var path4 = Path.Combine(Server.MapPath("~/Images/Products/"), fileName4); 
      file4.SaveAs(path4); 

      @product.Image4 = Url.Content("~/Images/Products/" + fileName4); 
     } 

     db.Product.Add(product); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    string domain = Request.Url.Host; 
    int clientid = (from a in db.Client where a.Domain == domain select a.ID).First(); 

    List<SelectListItem> categories = new List<SelectListItem>(); 
    foreach (var cat in db.Category.Where(c => c.ClientID == clientid)) 
    { 
     categories.Add(new SelectListItem() { Text = cat.Name, Value = cat.CategoryID.ToString() }); 
     } 
    ViewBag.Categories = categories; 
    return View(product); 
} 

之外,而你在它..既然你還缺少MaxID值,你可以只移動Viewbag代碼到一個單獨的方法所以您只需在返回視圖之前調用該方法。

private void LoadCreateViewBagData() 
{ 
    string domain = Request.Url.Host; 
    int clientid = (from a in db.Client where a.Domain == domain select a.ID).First(); 
    int maxID = db.Product.Where(c => c.ClientID == clientid).Max(c => (int?)c.ProductID) ?? 0; 

    ViewBag.MaxID = maxID + 1; 

    List<SelectListItem> categories = new List<SelectListItem>(); 
    foreach (var cat in db.Category.Where(c => c.ClientID == clientid)) 
    { 
     categories.Add(new SelectListItem() { Text = cat.Name, Value = cat.CategoryID.ToString() }); 
    } 

    ViewBag.Categories = categories; 
} 

// GET: Admin/Products/Create 
public ActionResult Create() 
{ 
    LoadCreateViewBagData(); 
    return View(); 
} 


[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "ID,ProductID,Name,CategoryID,Price,Promotion,Image1,Image2,Image3,Image4,Description,ClientID")] Product product, HttpPostedFileBase file1, HttpPostedFileBase file2, HttpPostedFileBase file3, HttpPostedFileBase file4) 
{ 
    if (ModelState.IsValid) 
    { 
     //yada yada 
    } 
    LoadCreateViewBagData(); 
    return View(product); 
} 
+0

它沒有錯誤地工作,但仍然沒有保存到數據庫。 – Ashiv3r

+0

這是因爲你的模型是無效的..你可以在if(ModelState.IsValid)上放一個斷點,並找出錯誤的可能性。可能性是你的一個非空字段爲空。 – JamieD77

+0

我所有的當地人都很好,file1-4可以爲null。 – Ashiv3r

相關問題