有型无ViewData的项目'的IEnumerable< SelectListItem>'具有关键"键" [英] There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key "key"

查看:138
本文介绍了有型无ViewData的项目'的IEnumerable< SelectListItem>'具有关键"键"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code是这样

 namespace DiagnosisApp.Models
{
    public class ProcedurePrice
    {
        public int ID { get; set; }


        public int DepartmentID { get; set; }
        public int ProcedureID { get; set; }
        public int InsuranceProviderID { get; set; }
        public int ProcedureCategoryID { get; set; }
        public int ProcedureSubCategoryID { get; set; }



        [ForeignKey("ID")]
        public virtual Department Department { get; set; }

        [ForeignKey("ID")]
        public virtual InsuranceProvider InsuranceProvider { get; set; }

        [ForeignKey("ID")]
        public virtual ProcedureCategory ProcedureCategory { get; set; }
        [ForeignKey("ID")]
        public virtual ProcedureSubCategory ProcedureSubCategory { get; set; }
    }
}

控制器

  public ActionResult Create()
        {
            ViewBag.DepartmentID = new SelectList(db.Departments, "ID", "Name");
            ViewBag.ProcedureSubCategoryID = new SelectList(db.ProcedureSubCategories, "ID", "Name");
            return View();
        }

在我看来

 @Html.DropDownList("ProcedureID", null, new { @class = "form-control" })
  @Html.ValidationMessageFor(model => model.ProcedureID)

一切看起来还好我,但它抛出一个错误

Everything looks okay to me, But it throws an error

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ProcedureID'.

任何人都可以指出我在做什么错在这里?

Can anyone point out what I am doing wrong here?

推荐答案

这个错误是你把它放在 ViewBag.ProcedureSubCategoryID ,而你是通过 ProcedureID INT Html.DropDownList(),你也正在传递的SelectList 参数。一个快速的解决办法是只需更换 ProcedureSubCategoryID ProcedureID Html.DropDownList()作为第一个参数键:

The mistake is you are putting it in ViewBag.ProcedureSubCategoryID while you are passing ProcedureID int Html.DropDownList() and also you are passing SelectList parameter null. A quick fix is to just replace ProcedureSubCategoryID with ProcedureID in Html.DropDownList() as key in first parameter:

您有三种方式来解决这个问题。

you have three ways to resolve this.

不是传递的SelectList ,它接受的类型,你可以做这样的事情第二个参数:

Instead of passing null in second parameter which accepts of type SelectListyou can do something like this:

@Html.DropDownList("ProcedureID", ViewBag.ProcedureSubCategoryID as SelectList, new  { @class="form-data" })

Way2

public ActionResult Create()
{
  ViewBag.DepartmentID = new SelectList(db.Departments, "ID", "Name");
  ViewBag.ProcedureSubCategoryID = new SelectList(db.ProcedureSubCategories, "ID", "Name");
  return View();
}


@Html.DropDownList("ProcedureSubCategoryID", null, new { @class = "form-control" })

方式三:

或替代的是它存储在 ProcedureID 在你的行动:

public ActionResult Create()
{
       ViewBag.DepartmentID = new SelectList(db.Departments, "ID", "Name");
       ViewBag.ProcedureID = new SelectList(db.ProcedureSubCategories, "ID", "Name");
       return View();
}

和中查看:

@Html.DropDownList("ProcedureID", null, new { @class = "form-control" })

这篇关于有型无ViewData的项目'的IEnumerable&LT; SelectListItem&GT;'具有关键&QUOT;键&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆