Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从强类型模型创建Html.DropDownList或Html.DropDownListFor_C#_Asp.net Mvc_Linq_Html Helper - Fatal编程技术网

C# 如何从强类型模型创建Html.DropDownList或Html.DropDownListFor

C# 如何从强类型模型创建Html.DropDownList或Html.DropDownListFor,c#,asp.net-mvc,linq,html-helper,C#,Asp.net Mvc,Linq,Html Helper,我有一个强类型模型 @model IPACS.Models.IPACS_Departments 此型号有部门ID、名称、说明、所有者。 我需要从这个模型中填充一个DropDownList,但我不确定如何填充 我希望值是部门ID,文本是名称 这是我正在使用的模型 那么,我的模型就是问题所在吗 公共IPACS_部门() { this.IPACS_Functions=new HashSet(); this.IPACS_Document=new HashSet(); } [Key]public

我有一个强类型模型

@model IPACS.Models.IPACS_Departments
此型号有
部门ID、名称、说明、所有者。

我需要从这个模型中填充一个DropDownList,但我不确定如何填充

我希望值是
部门ID
,文本是
名称

这是我正在使用的模型

那么,我的模型就是问题所在吗

公共IPACS_部门() { this.IPACS_Functions=new HashSet(); this.IPACS_Document=new HashSet(); }

    [Key]public int departmentID { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string owner { get; set; }

    public virtual ICollection<IPACS_Functions> IPACS_Functions { get; set; }
    public virtual ICollection<IPACS_Document> IPACS_Document { get; set; }
[Key]public int departmentID{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共字符串所有者{get;set;}
公共虚拟ICollection IPACS_函数{get;set;}
公共虚拟ICollection IPACS_文档{get;set;}
我尝试了下面答案中的语法,但无法编译


我的model.departmentID、model.name、model.description、model.owner我不确定我在这里遗漏了什么。

关于这方面有很多帖子和文档。这里有一些东西可以帮助你:

model.YourId,新选择列表(model.YourList,“ValuePath”,“TextPath”))%%>

dropdownlist应该有多个选项,而不仅仅是文本。或者我误解了什么?他可能是说他的模型包含了一个包含前面提到的字段的偏差列表@詹姆斯:您必须用数据填充
选择列表
,并将其与
下拉列表
一起使用。请参考下面的答案或查找具有此知识的示例,以了解正确的语法。我通读了一些示例,但我的模型或某些东西肯定是错误的,因为语法在我这方面不起作用。您正在使用razor。是的,我已经做过了。但它总是给我一条红色的曲线,表示它不喜欢四种不同的东西。我真的很讨厌使用viewbag,但是使用viewbag似乎简单多了。
<%= Html.DropDownListFor(model => model.YourId, new SelectList(Model.YourList, "ValuePath", "TextPath"))%>