Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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# 如何让DropDownList与IEnumerable一起使用<;选择列表>;使用cshtml页面?_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# 如何让DropDownList与IEnumerable一起使用<;选择列表>;使用cshtml页面?

C# 如何让DropDownList与IEnumerable一起使用<;选择列表>;使用cshtml页面?,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我的下拉列表可以显示,但它总是空的 我尝试过使用ViewBag、DropDownList和List,但这些都不能填充我的下拉列表 模型 public IEnumerable AccountTypeCheck{get;set;} 控制器 ViewBag.AccountTypeECheck = new List<SelectListItem>() { new SelectListItem() { Value = "creditCard",

我的下拉列表可以显示,但它总是空的

我尝试过使用ViewBag、DropDownList和List,但这些都不能填充我的下拉列表

模型

public IEnumerable AccountTypeCheck{get;set;}
控制器

ViewBag.AccountTypeECheck = new List<SelectListItem>() { 
    new SelectListItem() { 
        Value = "creditCard", 
        Text = "creditCard" 
    }
};
ViewBag.AccountTypeCheck=新列表(){
新建SelectListItem(){
Value=“信用卡”,
Text=“信用卡”
}
};
看法

@Html.DropDownList(
“价值”,
(IEnumerable)ViewBag.AccountTypeCheck,
无效的
新建{@class=“form control”}
)
试试这个:

控制器:

var selectListItems = new List<SelectListItem>() { new SelectListItem() { Value = "creditCard", Text = "creditCard" } };
ViewBag.AccountTypeECheck = new SelectList(selectListItems, "Value", "Text");
使用
ViewBag
的属性名称(在您的示例中为
accounttypecheck
)作为
DropDownList的
name
参数

参考资料:

试试这个:

控制器:

var selectListItems = new List<SelectListItem>() { new SelectListItem() { Value = "creditCard", Text = "creditCard" } };
ViewBag.AccountTypeECheck = new SelectList(selectListItems, "Value", "Text");
使用
ViewBag
的属性名称(在您的示例中为
accounttypecheck
)作为
DropDownList的
name
参数


参考:

如果您缺少模型成员,请尝试以下操作:

型号:

public IEnumerable<SelectListItem> AccountTypeECheck { get; set; }

public string AccountType {get;set;}

如果缺少模型成员,请尝试以下操作:

型号:

public IEnumerable<SelectListItem> AccountTypeECheck { get; set; }

public string AccountType {get;set;}

什么是
?什么是
?感谢代码示例。不过,这仍然存在空白的问题。感谢您提供的代码示例。不过,这仍然存在着同样的空白问题。我觉得这应该奏效。但是,它仍然是一个空下拉列表。我检查了服务,它肯定返回了AccountTypeCheck模型中的列表项,该模型的类型为System.Web.Mvc.SelectListItem。那不是正确的类型吗?不过,我假设SelectList将在模型内部的Items属性上迭代,但我可能弄错了。在DropDownList for上放置断点,并确保
model.AccountTypeCheck
包含数据。啊,我知道发生了什么。在返回模型之前,我正在序列化该模型,而该过程实际上失败了(尽管是无声的)。谢谢你的精神检查!我觉得这应该管用。但是,它仍然是一个空下拉列表。我检查了服务,它肯定返回了AccountTypeCheck模型中的列表项,该模型的类型为System.Web.Mvc.SelectListItem。那不是正确的类型吗?不过,我假设SelectList将在模型内部的Items属性上迭代,但我可能弄错了。在DropDownList for上放置断点,并确保
model.AccountTypeCheck
包含数据。啊,我知道发生了什么。在返回模型之前,我正在序列化该模型,而该过程实际上失败了(尽管是无声的)。谢谢你的精神检查!
public IEnumerable<SelectListItem> AccountTypeECheck { get; set; }

public string AccountType {get;set;}
var model = new Model();
model.AccountTypeECheck = new List<SelectListItem>() { 
    new SelectListItem() { 
        Value = "creditCard", 
        Text = "creditCard" 
    }
};

return View(model);
@model Model
@Html.DropDownListFor(x => x.AccountType, Model.AccountTypeECheck)