c# - 具有键 'CategoryId' 的 ViewData 项的类型为 'System.Int32' 但必须为 'IEnumerable<SelectListItem>' 类型?

标签 c# asp.net-mvc model-view-controller

所以我的代码之前是有效的。我不知道我做了什么让这件事发生,而且我似乎无法修复它。我见过有人说要重置 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; }

最佳答案

如果您的 ModelState 在您的 POST 操作中无效,您需要重新填充您的 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/

相关文章:

c# - WPF 控件,节省空间

c# - 如何在 C# 中调整图像大小?

javascript - Titanium 保存最后查看的窗口的状态

java - Spring Security 用户详细信息最佳实践

c# - 为什么我无法使用 Azure 服务总线队列获取队列消息?

c# - DataGridView:复制完整到剪贴板

c# - 在 C# 中将可变大小的十六进制字符串转换为有符号数(可变大小字节)

asp.net-mvc - 如何在 ASP.NET MVC/C#/LINQ/SQL 中的菜单上显示类别和子类别

jquery - "nested"MVC 中的表单

php - 在codeigniter中组合2个sql查询