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

问题描述

所以,我的code的工作之前。我不知道我做了什么要做到这一点,我似乎无法修复它。我见过的人都说重置的ModelState。 (ModelState.Clear();),但是这并没有帮助。此外,它没有帮助,我还是相当新的MVC。任何帮助将是AP preciated。谢谢你。

控制器:

 公众的ActionResult的Create()
    {
        ActiveDirectoryModel ADM =新ActiveDirectoryModel();
        ViewBag.notifyto = adm.FetchContacts();
        VAR模型=填充();
        返回查看(模型);
    }    [HttpPost]
    公众的ActionResult创建(CreateViewModel模型)
    {
        如果(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();
            返回RedirectToAction(「指数」);
        }        返回查看(模型);
    }    公共CreateViewModel填充()
    {
        ActiveDirectoryModel ADM =新ActiveDirectoryModel();
        VAR模型=新CreateViewModel
        {
            AssignedToItems = adm.FetchContacts(),
            CoAssignedToItems = adm.FetchContacts(),
            NotifyToItems = adm.FetchContacts(),
            CategoryItems =
                在新IntraEntities()C。CategoryItems.ToList()
                选择新SelectListItem
                {
                    文= c.Name,
                    值= c.ID.ToString()
                },
            SubCategoryItems =
                在新IntraEntities()SC。SubCategoryItems.ToList()
                选择新SelectListItem
                {
                    文= sc.Name,
                    值= sc.ID.ToString()
                },
            StatusItems =
                从s新IntraEntities()。StatusItems.ToList()
                其中,s.IsPriority ==假
                选择新SelectListItem
                {
                    文= s.Name,
                    值= s.ID.ToString()
                },
            PriorityItems =
                在新IntraEntities()第StatusItems.ToList()
                其中,p.IsPriority ==真
                选择新SelectListItem
                {
                    文= p.Name,
                    值= p.ID.ToString()
                }
        };
        回归模型;
    }

查看:

 < D​​IV CLASS =createTopInner>
    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.leaf.Category)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.DropDownListFor(型号=> model.CategoryId,Model.CategoryItems,)
        @ Html.ValidationMessageFor(型号=> model.leaf.Category)
    < / DIV>
< / DIV>

型号:

 公众诠释的CategoryId {搞定;组; }
    公共IEnumerable的< SelectListItem> CategoryItems {搞定;组; }


解决方案

如果您的ModelState是不是在你的POST操作有效,需要重新填充你的SelectList属性:

 如果(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.

这篇关于具有关键的“类别ID”的ViewData的产品类型'System.Int32'的,但必须是类型为“IEnumerable的&LT; SelectListItem&GT;'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 18:23