C# 如何从动态创建的ViewBag中的封装属性创建DropDownList?

C# 如何从动态创建的ViewBag中的封装属性创建DropDownList?,c#,asp.net-mvc-4,viewdata,viewbag,C#,Asp.net Mvc 4,Viewdata,Viewbag,注意:变量名称已更改,以保持代码的匿名性 任务: /* db.Companies is DataContext (Entities), * CarShow.Company.Id is a ViewModel (Company is encapsulated under CarShow) */ ViewBag.CompanyList = new SelectList( db.Companies, //Constructor

注意:变量名称已更改,以保持代码的匿名性

任务

/* db.Companies is DataContext (Entities), 
*  CarShow.Company.Id is a ViewModel (Company is encapsulated under CarShow) */
ViewBag.CompanyList = new SelectList( db.Companies,       //Constructor
                                      "Id",               //Property Name
                                      "Name",             //Text to display each item
                                      CarShow.Company.Id  //Value of the initially selected item
                                      ).Distinct();
创建一个
SelectList
,该列表将显示所选内容(如果选择了任何内容),并将生成一个可供选择的列表

ViewBag
CarShow
控制器中声明/定义

/* db.Companies is DataContext (Entities), 
*  CarShow.Company.Id is a ViewModel (Company is encapsulated under CarShow) */
ViewBag.CompanyList = new SelectList( db.Companies,       //Constructor
                                      "Id",               //Property Name
                                      "Name",             //Text to display each item
                                      CarShow.Company.Id  //Value of the initially selected item
                                      ).Distinct();
我在“编辑视图”中尝试了以下代码,以生成带有错误消息的
选择列表:

/* <-- The ViewData item that has the key 'Company.Id' is of 
    type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'. --> */
@Html.DropDownListFor( model => model.Company.Id,
                      (IEnumerable<SelectListItem>)ViewBag.CompanyList )

/* <!-- Compilation Error --> */
@Html.DropDownList( (IEnumerable<SelectListItem>)ViewData.Id,
                    (IEnumerable<SelectListItem>)ViewData.Name )

/* <!-- The ViewData item that has the key 'Id' is of type 'System.Int32' 
     but must be of type'IEnumerable<SelectListItem>'. --> */
@Html.DropDownList( "Id", 
                    (IEnumerable<SelectListItem>)ViewData["Name"], 
                     new { id = "Id" })

/* <!-- Cannot apply indexing with [] to an 
     expression of type 'System.Dynamic.DynamicObject' --> */
@Html.DropDownList("Id", (IEnumerable<SelectListItem>)ViewBag["Name"])
/**/
@Html.DropDownListFor(model=>model.Company.Id,
(IEnumerable)ViewBag.CompanyList)
/*  */
@Html.DropDownList((IEnumerable)ViewData.Id,
(IEnumerable)ViewData.Name)
/*  */
@Html.DropDownList(“Id”,
(IEnumerable)视图数据[“名称”],
新的{id=“id”})
/*  */
@Html.DropDownList(“Id”,(IEnumerable)ViewBag[“Name”])
我的方法正确吗?我的“编辑视图”代码或我的
CarShow
控制器和“编辑视图”代码错误吗?


我想提前向任何提供帮助的人表示感谢。

试试这个,我想你不想区分列表

ViewBag.CompanyList = new SelectList( db.Companies.Distinct().ToList(),      
                                      "Id",             
                                      "Name",             
                                      CarShow.Company.Id);


@Html.DropDownList("CompanyList", (SelectList)ViewBag.CompanyList)

在您的视图中,使用强类型版本:

@Html.DropDownListFor( model => model.Company.Id, (SelectList)ViewBag.CompanyList )
在控制器中,从行尾移除
.Distinct()
。。。如果您需要,可以这样使用:

ViewBag.CompanyList = new SelectList( db.Companies.Distinct(), "Id",  "Name", CarShow.Company.Id );

在GET请求之后,您是否出现了该错误?我得到了以下错误:数据绑定:“System.Data.Entity.DynamicProxies.Faculture_260FAB836DB2F81BEC4B2B820E387F9BCB08D23B1F11DE5302BDE931EB40491”不包含名为“name”的属性。可能您的属性调用的不是“name”?上面写着“教员”。。。教员是否有名为“Name”的财产?@Romias感谢您帮助我解决了您的评论中的一个问题!我无意中为
DataTextField
添加了错误的值!我得到的错误是:DataBinding:“System.Data.Entity.DynamicProxies.Faculty_260FAB836DB2F81BEC4B820E387F9BCB08D23B13B1F11DE5302BDE931EB40491”不包含名为“name”的属性。出现此错误的原因是,您的表中没有名为name的属性文件。我不知道,我以为它会更改它,现在可以了。非常感谢。