具有键';属性';属于';System.String';但必须是';IEnumerable

本文关键字:IEnumerable String 属于 属性 System | 更新日期: 2023-09-27 18:21:15

我正在Dropdownlist中绑定Categories和Gender。我的模型有两个属性映射到其他表中的列:

        [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; }

我的产品控制器有一个名为"创建"的方法,用于上传

 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")
}

当我提交表格时,我在上面提到的"类别"处遇到了错误。我检查了StackOverFlow,但没有找到有用的信息。我真的很想知道发生了什么,我哪里做错了。提前感谢您的帮助

具有键';属性';属于';System.String';但必须是';IEnumerable

在类中添加一个新属性:

型号:

[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在评论中所说,当ModelState无效时,您需要将值重新分配给CategoryList属性。

[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>

在"性别"中执行相同操作