键为'CategoryId'的ViewData项类型为'System.Int32'但类型

本文关键字:类型 System Int32 ViewData CategoryId 键为 | 更新日期: 2023-09-27 18:18:37

所以我的代码之前是工作的。我不知道我做了什么让这一切发生,我似乎无法解决它。我看到有人说要重置ModelState。(ModelState.Clear();)但这无济于事。此外,我对MVC还是相当陌生,这也没有帮助。任何帮助都会很感激。谢谢。

控制器:

    public ActionResult Create()
    {
        ActiveDirectoryModel adm = new ActiveDirectoryModel();
        ViewBag.notifyto = adm.FetchContacts();
        var model = Populate();

        return View(model);
    } 
    [HttpPost]
    public ActionResult Create(CreateViewModel model)
    {
        if (ModelState.IsValid)
        {
            model.leaf.Date = DateTime.Now.Date;
            model.leaf.Category = model.CategoryId;
            model.leaf.SubCategory = model.SubCategoryId;
            model.leaf.AssignedTo = model.AssignedToId;
            model.leaf.CoAssignedTo = model.CoAssignedToId;
            model.leaf.Status = model.StatusId;
            model.leaf.Priority = model.PriorityId;
            //model.lead.Parent = model.ParentID;
            db.LeafItems.AddObject(model.leaf);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(model);
    }
    public CreateViewModel Populate()
    {
        ActiveDirectoryModel adm = new ActiveDirectoryModel();
        var model = new CreateViewModel
        {
            AssignedToItems = adm.FetchContacts(),
            CoAssignedToItems = adm.FetchContacts(),
            NotifyToItems = adm.FetchContacts(),
            CategoryItems =
                from c in new IntraEntities().CategoryItems.ToList()
                select new SelectListItem
                {
                    Text = c.Name,
                    Value = c.ID.ToString()
                },
            SubCategoryItems =
                from sc in new IntraEntities().SubCategoryItems.ToList()
                select new SelectListItem
                {
                    Text = sc.Name,
                    Value = sc.ID.ToString()
                },
            StatusItems =
                from s in new IntraEntities().StatusItems.ToList()
                where s.IsPriority == false
                select new SelectListItem
                {
                    Text = s.Name,
                    Value = s.ID.ToString()
                },
            PriorityItems =
                from p in new IntraEntities().StatusItems.ToList()
                where p.IsPriority == true
                select new SelectListItem
                {
                    Text = p.Name,
                    Value = p.ID.ToString()
                }
        };
        return model;
    }

视图:

<div class="createTopInner">
    <div class="editor-label">
        @Html.LabelFor(model => model.leaf.Category)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.CategoryId, Model.CategoryItems, "")
        @Html.ValidationMessageFor(model => model.leaf.Category)
    </div>
</div>

模型:

    public int CategoryId { get; set; }
    public IEnumerable<SelectListItem> CategoryItems { get; set; }

键为'CategoryId'的ViewData项类型为'System.Int32'但类型

如果您的ModelState对POST操作无效,您需要重新填充您的SelectList属性:

if( ModelState.IsValid ) 
{
    // save and redirect
    // ...
}
// repopulate your SelectList properties:
model.CategoryItems = GetCategories();
return View(model);

不要重新填充整个模型,否则您可能会丢失用户所做的任何更改。

我找到解决方案的另一种方法是采取隐藏字段。所以在你的情况下,你可以这样做:

@Html.HiddenFor(model => model.CategoryId)

现在用另一个字段表单也张贴和设置各自模型中的CategoryId

相关文章: