C# 使用值填充DropDownList for

C# 使用值填充DropDownList for,c#,asp.net-mvc,drop-down-menu,html.dropdownlistfor,C#,Asp.net Mvc,Drop Down Menu,Html.dropdownlistfor,我是MVC的初学者,正在尝试用值填充DropDownListFor标记 下面是我现在发现的帮助我构建代码的内容: 因此,在我的控制器中现在有两个类,一个是主类(ObjController:controller),另一个是我根据上面的示例创建的类 另一个类如下所示: public class ItemCostViewModel { public int Id { get; set; } [Required] [Display(Name = "Item Cost :")]

我是MVC的初学者,正在尝试用值填充
DropDownListFor
标记

下面是我现在发现的帮助我构建代码的内容:

因此,在我的控制器中现在有两个类,一个是主类
(ObjController:controller)
,另一个是我根据上面的示例创建的类

另一个类如下所示:

public class ItemCostViewModel
{
    public int Id { get; set; }

    [Required]
    [Display(Name = "Item Cost :")]
    public string ItemCostCode { get; set; }

    public IEnumerable<ItemCostViewModel> GetItemCosts()
    {
        return new List<ItemCostViewModel>
        {
            new ItemCostViewModel() {Id = 1, ItemCostCode = "One"},
            new ItemCostViewModel() {Id = 2, ItemCostCode = "Two"},
            new ItemCostViewModel() {Id = 3, ItemCostCode = "Three"},
            new ItemCostViewModel() {Id = 4, ItemCostCode = "Four"},
            new ItemCostViewModel() {Id = 5, ItemCostCode = "Five"},
            new ItemCostViewModel() {Id = 6, ItemCostCode = "Six"}
        };
    }
}
Dictionary<string, int> dictObjCost = new Dictionary<string, int>
            {
                {"One", 1},
                {"Two", 2},
                {"Three", 3},
                {"Four", 4},
                {"Five", 5},
                {"Six", 6}
            };

            ViewBag.itemCostValue = new SelectList(dictObjCost, "Value", "Key");
但遗憾的是,由于
ItemCostCode
不在控制器中,它会引发崩溃:

Compiler Error Message: CS1061:'System.Collections.Generic.IEnumerable<Project1.Models.ObjectInfo>' does not containt a definition for "ItemCostCode" (...)
编译器错误消息:CS1061:“System.Collections.Generic.IEnumerable”不包含“ItemCostCode”的定义(…)
我很困惑,我不知道我现在在做什么,怎么做。有人能帮我解释一下哪里出了问题,和/或如何纠正吗


非常感谢

以下是我建议您解决问题的方法:

1) 将
GetItemCosts()
方法的签名更改为返回列表。不管怎样,您都在这样做,在后续步骤中会更容易

2) 在控制器中设置以下内容(使用调用
GetItemCosts()
)所做的任何操作):

var itemCodeList=newlist();
GetItemCosts().ForEach(ic=>itemCodeList.Add(新的DropDownItem{Value=ic.ID,Text=ic.ItemCostCode});
ViewData[“ItemCodes”]=itemCodeList;
3) 视图中的行现在应该可以如下所示:

@Html.DropDownListFor(m => m.ItemCostCode , ((IEnumerable<DropDownItem)(ViewData["ItemCodes"])))

@Html.DropDownListFor(m=>m.ItemCostCode,((IEnumerable好的,最后,我必须承认你们都是对的

下面是我所做的,它比我所做的代码要简单得多:

在我的控制器中,我创建了一个属于
ViewBag
的词汇表,如下所示:

public class ItemCostViewModel
{
    public int Id { get; set; }

    [Required]
    [Display(Name = "Item Cost :")]
    public string ItemCostCode { get; set; }

    public IEnumerable<ItemCostViewModel> GetItemCosts()
    {
        return new List<ItemCostViewModel>
        {
            new ItemCostViewModel() {Id = 1, ItemCostCode = "One"},
            new ItemCostViewModel() {Id = 2, ItemCostCode = "Two"},
            new ItemCostViewModel() {Id = 3, ItemCostCode = "Three"},
            new ItemCostViewModel() {Id = 4, ItemCostCode = "Four"},
            new ItemCostViewModel() {Id = 5, ItemCostCode = "Five"},
            new ItemCostViewModel() {Id = 6, ItemCostCode = "Six"}
        };
    }
}
Dictionary<string, int> dictObjCost = new Dictionary<string, int>
            {
                {"One", 1},
                {"Two", 2},
                {"Three", 3},
                {"Four", 4},
                {"Five", 5},
                {"Six", 6}
            };

            ViewBag.itemCostValue = new SelectList(dictObjCost, "Value", "Key");
现在我的值显示为“1”,“2”等等

当我获得视图时,我传递了'
int?itemCostValue
'参数,一切都正常工作


感谢大家让我意识到我在做什么:)

首先,在MVC中,您应该在控制器中创建列表,然后将它们作为模型传递给视图,或者(在您的情况下)通过ViewData(或ViewBag)传递给视图动态变量。第二,您需要实际将项目转换为SelectListItem或DropDownItem的IEnumerable。这就是我到目前为止所做的。但当我遇到问题时,人们一直告诉我不应该使用DropDownList,而应该使用DropDownListFor(我还没有弄清楚DropDownListFor是如何工作的)。此外,我希望能够显示某些内容(字符串)并获得一个值(int)。
Item Cost : @Html.DropDownList("itemCostValue ", String.Empty)