具有键“CategoryId"的 ViewData 项的类型为“System.Int32",但必须为“IEnumerable<SelectListItem>"类型? [英] The ViewData item that has the key &#39;CategoryId&#39; is of type &#39;System.Int32&#39; but must be of type &#39;IEnumerable&lt;SelectListItem&gt;&#39;?

查看:20
本文介绍了具有键“CategoryId"的 ViewData 项的类型为“System.Int32",但必须为“IEnumerable<SelectListItem>"类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的代码以前是有效的.我不知道我做了什么导致这种情况发生,而且我似乎无法解决它.我见过有人说要重置 ModelState.( ModelState.Clear(); ) 但这并没有帮助.此外,我对 MVC 还很陌生,这也无济于事.任何帮助,将不胜感激.谢谢.

控制器:

 public ActionResult Create(){ActiveDirectoryModel adm = new ActiveDirectoryModel();ViewBag.notifyto = adm.FetchContacts();var 模型 = Populate();返回视图(模型);}[HttpPost]公共 ActionResult 创建(CreateViewModel 模型){如果(模型状态.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("索引");}返回视图(模型);}公共 CreateViewModel 填充(){ActiveDirectoryModel adm = new ActiveDirectoryModel();var 模型 = 新的 CreateViewModel{AssignedToItems = adm.FetchContacts(),CoAssignedToItems = adm.FetchContacts(),NotifyToItems = adm.FetchContacts(),类别项目 =来自 c in new IntraEntities().CategoryItems.ToList()选择新的 SelectListItem{文字 = c.Name,值 = c.ID.ToString()},子类别项目 =来自新 IntraEntities().SubCategoryItems.ToList() 中的 sc选择新的 SelectListItem{文本 = sc.Name,值 = sc.ID.ToString()},状态项 =from s in new IntraEntities().StatusItems.ToList()其中 s.IsPriority == false选择新的 SelectListItem{文字 = s.Name,值 = s.ID.ToString()},优先事项 =从 p 在 new IntraEntities().StatusItems.ToList()其中 p.IsPriority == true选择新的 SelectListItem{文字 = p.Name,值 = p.ID.ToString()}};退货模式;}

查看:

<div class="editor-label">@Html.LabelFor(model => model.leaf.Category)

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

型号:

 public int CategoryId { get;放;}公共 IEnumerable类别项目 { 获取;放;}

解决方案

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

if( ModelState.IsValid ){//保存并重定向//...}//重新填充您的 SelectList 属性:model.CategoryItems = GetCategories();返回视图(模型);

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

So my code was working before. I don't know what I did for this to happen and I can't seem to fix it. I've seen people say to reset the ModelState. ( ModelState.Clear(); ) But that didn't help. Also, it doesn't help that I'm still fairly new to MVC. Any help would be appreciated. Thanks.

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

View:

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

Model:

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

解决方案

If your ModelState is not valid on your POST action, you need to repopulate your SelectList properties:

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

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

return View(model);

Do not repopulate the entire model because otherwise you could potentially lose any changes that the user made.

这篇关于具有键“CategoryId"的 ViewData 项的类型为“System.Int32",但必须为“IEnumerable<SelectListItem>"类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆