
[英]There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key
[英]There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key country
在 MVC 中绑定下拉列表时,我总是收到此错误: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key country
。
看法
@Html.DropDownList("country", (IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")
控制器
List<Companyregister> coun = new List<Companyregister>();
coun = ds.getcountry();
List<SelectListItem> item8 = new List<SelectListItem>();
foreach( var c in coun )
{
item8.Add(new SelectListItem
{
Text = c.country,
Value = c.countryid.ToString()
});
}
ViewBag.countrydrop = item8;
return View();
我不知道如何解决。
在您的操作中,将ViewBag.countrydrop = item8
更改为ViewBag.country = item8;
并在视图中这样写:
@Html.DropDownList("country",
(IEnumerable<SelectListItem>)ViewBag.country,
"Select country")
其实你写的时候
@Html.DropDownList("country", (IEnumerable)ViewBag.country, "选择国家")
或者
Html.DropDownList("国家","选择国家)
它在ViewBag
使用 key country查找IEnumerable<SelectListItem>
,您也可以在这种情况下使用此重载:
@Html.DropDownList("country","Select country") // it will look for ViewBag.country and populates dropdown
请参阅工作演示示例
如果您像这样使用DropDownListFor
:
@Html.DropDownListFor(m => m.SelectedItemId, Model.MySelectList)
其中模型中的MySelectList
是SelectList
类型的属性,如果该属性为null
则可能会引发此错误。
通过在构造函数中简单初始化它来避免这种情况,如下所示:
public MyModel()
{
MySelectList = new SelectList(new List<string>()); // empty list of anything...
}
我知道这不是 OP 的情况,但这可能会帮助像我这样因这个而出现相同错误的人。
尝试这个
@Html.DropDownList("ddlcountry",(List<SelectListItem>)ViewBag.countrydrop,"Select country")
在控制器中
ViewBag.countrydrop = ds.getcountry().Select(x => new SelectListItem { Text = x.country, Value = x.countryid.ToString() }).ToList();
尝试这个。
控制器:
List<CountryModel> countryList = db.countryTable.ToList();
ViewBag.Country = new SelectList(countryList, "Country", "CountryName");
你可以使用这个:
var list = new SelectList(countryList, "Id", "Name");
ViewBag.countries=list;
@Html.DropDownList("countries",ViewBag.countries as SelectList)
我遇到了同样的问题,我找到了解决方案,即我应该在 Edit Method 中放置代码以从数据库中检索下拉列表。 它对我有用。 类似问题的解决方案
就我而言,发生错误是因为我没有像这样初始化控制器中的选择列表:
viewModel.MySelectList = new List<System.Web.Mvc.SelectListItem>();
由于现有的答案都没有让我清楚这一点,所以我发布了这个。 也许它可以帮助任何人。
您的代码是正确的,因为在某些情况下它不起作用,我也不知道错误来自哪里,但这可以帮助您解决问题,将代码移动到视图的头部,如下所示:
index.cshtml:
@using expert.Models;
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
Manager db = new Manager();
ViewBag.FORM_PRESTATION = new SelectList(db.T_BDE_PRESTATION_PRES.OrderBy(p => p.PRES_INTITULE).Where(p => p.T_B_PRES_ID == null), "PRES_ID", "PRES_INTITULE");
}
<div class="form-group">
<label class = "w3-blue" style ="text-shadow:1px 1px 0 #444">Domaine:</label>
<div class="col-md-10">
@Html.DropDownList("FORM_PRESTATION", null, htmlAttributes: new { @class = "w3-select ", @required = "true" })
</div>
</div>
在你的视图中用"countrydrop"
替换"country"
,就像这样......
@Html.DropDownList("countrydrop", (IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")
可能类似于 ASP.NET MVC 框架
@Html.DropDownList("country", ViewBag.countrydrop as List<SelectListItem>,"Select country")
或 ASP.NET Core MVC
<select class="class" id="country"
asp-items="@(new SelectLits(ViewBag.countrydrop,"Value","Text"))">
您面临这个问题是因为当您发布表单时,重新加载下拉列表后无法在 viewbag 中找到数据。 因此,请确保在从数据库或静态列表检索数据时在 get 方法中使用的代码,将该代码也复制粘贴到 post 动词中。
快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.