所以我的代码以前工作过。我不知道该怎么做,而且似乎无法解决。我见过有人说要重置ModelState。 (ModelState.Clear();)但这没有帮助。另外,我还是MVC的新手也无济于事。任何帮助,将不胜感激。谢谢。

Controller :

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

最佳答案

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

if( ModelState.IsValid )
{
    // save and redirect
    // ...
}

// repopulate your SelectList properties:
model.CategoryItems = GetCategories();

return View(model);

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

关于c# - 关键字为 'CategoryId'的ViewData项的类型为 'System.Int32',但必须类型为 'IEnumerable<SelectListItem>'?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8595332/

10-10 19:03