2015-11-10 72 views
1

我在Dropdownlist中綁定類別和性別。我的模型在其他表映射到列兩個屬性:具有'屬性'鍵的ViewData項的類型爲'System.String',但必須是'IEnumerable <SelectListItem>'的類型

 [Required(ErrorMessage = "Choose your category")] 
     public string Category { get; set; } 

     [ForeignKey("Category")] 
     [NotMapped] 
     public virtual Category Category_Name { get; set; } 

     [Required(ErrorMessage = "Choose your category")] 
     public string Gender { get; set; } 

     [ForeignKey("Gender")] 
     [NotMapped] 
     public virtual Gender Gender_Name { get; set; } 

我的ProductsController有一個名爲「創建」方法,它是用於上傳

public ActionResult Create() 
     { 
      ViewBag.Category = new SelectList(
       db.Categories.ToList(), 
       "Category_ID", 
       "Category_Name") 
       ; 
      ViewBag.Gender = new SelectList(
       db.Genders.ToList(), 
       "Gender_ID", 
       "Gender_Name" 
       ); 
      return View(); 
     } 

     // 
     // POST: /Products/Create 

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create(Products products) 
     { 
      if (products.Image.ContentLength > (2 * 1024 * 1024)) 
      { 
       ModelState.AddModelError("CustomError", "The Size of the 
      Image is 2MB"); 
       return View(); 
      } 
      if (!(products.Image.ContentType == "image/jpeg" || 
       products.Image.ContentType == "image/gif")) 
      { 
       ModelState.AddModelError("CustomError", "File type allowed : 
       jpeg and gif"); 
       return View(); 
      } 

      byte[] data = new byte[products.Image.ContentLength]; 
      products.Image.InputStream.Read(data, 0, 
      products.Image.ContentLength); 
      products.Product_Photo = data; 
      db.Products.Add(products); 
      db.SaveChanges(); 
      return View(products); 
     } 

相應的視圖:

@model eKart.Models.Products 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm("Create", "Products", FormMethod.Post, new { enctype 
       = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Products</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Product_Name) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Product_Name) 
      @Html.ValidationMessageFor(model => model.Product_Name) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Product_Quantity) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Product_Quantity) 
      @Html.ValidationMessageFor(model => model.Product_Quantity) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Product_Price) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Product_Price) 
      @Html.ValidationMessageFor(model => model.Product_Price) 
     </div> 
     <div> 
      @Html.LabelFor(model=>model.Product_Photo) 
     </div> 
     <div> 

      @Html.TextBoxFor(model=> model.Image, new{type="file"}) 
      @Html.ValidationMessage("CustomError") 
      </div> 

     <div class ="editor-label"> 
      @Html.LabelFor(model => model.Category) 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("Category", 
      (SelectList)ViewBag.Category,"Choose Category") 
      @Html.ValidationMessageFor(model=>model.Category) 
     </div> 
     <div class ="editor-label"> 
      @Html.LabelFor(model=>model.Gender) 
     </div> 
     <div class ="editor-field"> 
      @Html.DropDownList("Gender",(SelectList)ViewBag.Gender,"Choose 
      Gender") 
      @Html.ValidationMessageFor(model=>model.Gender) 
     </div> 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

當我提交我打的錯誤處提到above.I檢查計算器,但沒有找到有用的information.I真的想知道發生了什麼事情和W「分類」的形式在這裏我做wrong.Thanks您的幫助提前

+0

的錯誤是自我解釋真的下拉列表需要在SelectListItem不選擇列表的,改變你的viewbag.category是一個selectlistitem –

+0

我是mvc的初學者,請告訴我該怎麼做? –

+0

試試我的答案,這是他正在談論的。 –

回答

2

在你的類添加一個新的屬性:

型號:

[Required(ErrorMessage = "Choose your category")] 
public string Category { get; set; } 

public List<System.Web.Mvc.SelectListItem> CategoryList { get; set; } 

控制器:

public ActionResult Create() 
{ 
    var model = new Products(); 
    model.CategoryList = db.Categories.Select(x => new SelectListItem 
    { 
     Text = x.CategoryName, 
     Value = x.CategoryName 
    }).ToList(); 

    return View(model); 
} 

編輯:由於StephenMuecke在他的評論說,你需要的值重新分配給您的CategoryList屬性時ModelState無效。

[HttpPost] 
public ActionResult Create(Product product) 
{ 
    if(ModelState.IsValid) 
    { 
     // Code here 
    } 

    product.CategoryList = db.Categories.Select(x => new SelectListItem 
    { 
     Text = x.CategoryName, 
     Value = x.CategoryName 
    }).ToList(); 

    return View(product); 
} 

查看:

<div class ="editor-label"> 
    @Html.LabelFor(model => model.Category) 
</div> 
<div class="editor-field"> 
    @Html.DropDownListFor(model => model.Category, Model.CategoryList, "Choose category") 
    @Html.ValidationMessageFor(model=>model.Category) 
</div> 

做相同的「性別」

+0

這有什麼好做的問題(雖然它的良好做法)。參考重複內容以瞭解真正的問題。 –

+0

是值和文本具有相同的值是CategoryName? –

+0

@StephenMuecke謝謝,但他說,當他跑的項目,而不是提交表單後,他打的誤差。 –

相關問題