在mvc中的绑定下拉列表中我总是会收到此错误没有类型' IEnumerable'有钥匙的国家,我不知道如何分类
查看
@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();
我不知道我错在哪里,任何人都可以提前帮助我
答案 0 :(得分:16)
在您的操作中,将ViewBag.countrydrop = item8
更改为ViewBag.country = item8;
,然后在View中写下:
@Html.DropDownList("country",
(IEnumerable<SelectListItem>)ViewBag.country,
"Select country")
实际上当你写
<强> @ Html.DropDownList( “国家”, (IEnumerable的)ViewBag.country, “选择国家/地区”)
或
Html.DropDownList(“country”,“Select Country”
它在IEnumerable<SelectListItem>
中使用键国家查找ViewBag
,在这种情况下您也可以使用此重载:
@Html.DropDownList("country","Select country") // it will look for ViewBag.country and populates dropdown
答案 1 :(得分:12)
如果你这样使用DropDownListFor
:
@Html.DropDownListFor(m => m.SelectedItemId, Model.MySelectList)
其中模型中的MySelectList
是SelectList
类型的属性,如果属性为null
,则可能会抛出此错误。
通过在构造函数中简单地初始化它来避免这种情况,如下所示:
public MyModel()
{
MySelectList = new SelectList(new List<string>()); // empty list of anything...
}
我知道这不是OP的情况,但这可能会帮助像我这样的人因此而出现同样的错误。
答案 2 :(得分:2)
请注意,选择列表会发布为null,因此您的错误会导致无法找到viewdata属性。
始终在POST操作中重新初始化您的选择列表。
答案 3 :(得分:1)
使用此
var list = new SelectList(countryList, "Id", "Name");
ViewBag.countries=list;
@Html.DropDownList("countries",ViewBag.Roles as SelectList)
答案 4 :(得分:0)
试试这个
@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();
答案 5 :(得分:0)
我遇到了同样的问题,我找到了解决方案,我应该把代码放在编辑方法中从数据库中检索下拉列表。它对我有用。 Solution for the similar problem
答案 6 :(得分:0)
在我的情况下,发生错误是因为我没有在控制器中初始化选择列表,如下所示:
viewModel.MySelectList = new List<System.Web.Mvc.SelectListItem>();
由于现有的答案都没有向我说明,我发布了这个。也许它可以帮助任何人。
答案 7 :(得分:0)
你的代码是正确的,因为在某些情况下它不起作用,我也不知道错误来自哪里,但这可以帮助你解决问题,将代码移动到视图的头部,如下所示:
[86429.875904] # GUEST_ES_SEL = 0x0
[86429.875905] # GUEST_CS_SEL = 0x0
[86429.875905] # GUEST_SS_SEL = 0x0
[86429.875906] # GUEST_DS_SEL = 0x0
[86429.875907] # GUEST_FS_SEL = 0x0
[86429.875907] # GUEST_GS_SEL = 0x0
[86429.875908] # GUEST_LDTR_SEL = 0x0
[86429.875908] # GUEST_TR_SEL = 0x0
[86429.875909] # GUEST_INTERRUPT_STATUS = 0x0
[86429.876003] # GUEST_PHYSICAL_ADDR_FULL = 0x0
[86429.876004] # GUEST_PHYSICAL_ADDR_HIGH = 0x0
[86429.876006] # VMCS_LINK_PTR_FULL = 0xffffffffffffffff
[86429.876007] # VMCS_LINK_PTR_HIGH = 0xffffffff
[86429.876009] # GUEST_IA32_DEBUGCTL_FULL = 0x0
[86429.876010] # GUEST_IA32_DEBUGCTL_HIGH = 0x0
[86429.876012] # GUEST_IA32_PAT_FULL = 0x0
[86429.876013] # GUEST_IA32_PAT_HIGH = 0x0
[86429.876015] # GUEST_IA32_EFER_FULL = 0x0
[86429.876017] # GUEST_IA32_EFER_HIGH = 0x0
[86429.876018] # GUEST_IA32_PERF_CTL_FULL = 0x0
[86429.876020] # GUEST_IA32_PERF_CTL_HIGH = 0x0
[86429.876020] # GUEST_PDPTE0_FULL = 0x0
[86429.876021] # GUEST_PDPTE0_HIGH = 0x0
[86429.876022] # GUEST_PDPTE1_FULL = 0x0
[86429.876022] # GUEST_PDPTE1_HIGH = 0x0
[86429.876023] # GUEST_PDPTE2_FULL = 0x0
[86429.876024] # GUEST_PDPTE2_HIGH = 0x0
[86429.876025] # GUEST_PDPTE3_FULL = 0x0
[86429.876025] # GUEST_PDPTE3_HIGH = 0x0
[86429.876067] # EXIT_INTERRUPT_INFO = 0x0
[86429.876067] # EXIT_INTERRUPT_ERRCODE = 0x0
[86429.876068] # IDT_VECTORING_INFO_FIELD = 0x0
[86429.876069] # IDT_VECTORING_ERRCODE = 0x0
[86429.876069] # EXIT_INSTR_LEN = 0x0
[86429.876070] # INSTR_INFO = 0x0
[86429.876070] # GUEST_ES_LIMIT = 0xffff
[86429.876071] # GUEST_CS_LIMIT = 0xffff
[86429.876072] # GUEST_SS_LIMIT = 0xffff
[86429.876072] # GUEST_DS_LIMIT = 0xffff
[86429.876073] # GUEST_FS_LIMIT = 0xffff
[86429.876074] # GUEST_GS_LIMIT = 0xffff
[86429.876075] # GUEST_LDTR_LIMIT = 0xffff
[86429.876075] # GUEST_TR_LIMIT = 0xffff
[86429.876076] # GUEST_GDTR_LIMIT = 0xffff
[86429.876077] # GUEST_IDTR_LIMIT = 0xffff
[86429.876077] # GUEST_ES_ATTR = 0x93
[86429.876078] # GUEST_CS_ATTR = 0x9b
[86429.876078] # GUEST_SS_ATTR = 0x93
[86429.876079] # GUEST_DS_ATTR = 0x93
[86429.876080] # GUEST_FS_ATTR = 0x93
[86429.876080] # GUEST_GS_ATTR = 0x93
[86429.876081] # GUEST_LDTR_ATTR = 0x82
[86429.876081] # GUEST_TR_ATTR = 0x8b
[86429.876082] # GUEST_INTERRUPTIBILITY_INFO = 0x0
[86429.876084] # GUEST_ACTIVITY_STATE = 0x0
[86429.876085] # GUEST_SMBASE = 0x0
[86429.876086] # GUEST_IA32_SYSENTER_CS = 0x0
[86429.876087] # vmread(0x482E) failed
[86429.876088] # RFLAGS: 0x242
[86429.876088] # GUEST_PREEMTION_TIMER INVALID_VALUE
[86429.876090] # HOST_IA32_SYSENTER_CS = 0x10
[86429.876092] # CR0_MASK = 0xfffffffffffffff0
[86429.876093] # CR4_MASK = 0xfffffffffffff871
[86429.876095] # CR0_READ_SHADOW = 0x60000010
[86429.876096] # CR4_READ_SHADOW = 0x10
[86429.876097] # CR3_TARGET_0 = 0x0
[86429.876099] # CR3_TARGET_1 = 0x23341e000
[86429.876101] # CR3_TARGET_2 = 0x0
[86429.876102] # CR3_TARGET_3 = 0x0
[86429.876103] # EXIT_QUALIFICATION = 0x0
[86429.876104] # IO_RCX = 0x0
[86429.876104] # IO_RSI = 0x0
[86429.876105] # IO_RDI = 0x0
[86429.876105] # IO_RIP = 0x0
[86429.876106] # GUEST_LINEAR_ADDR = 0x0
[86429.876108] # GUEST_CR0 = 0x50032
[86429.876108] # GUEST_CR3 = 0x0
[86429.876109] # GUEST_CR4 = 0x2050
[86429.876110] # GUEST_ES_BASE = 0x0
[86429.876110] # GUEST_CS_BASE = 0x0
[86429.876111] # GUEST_SS_BASE = 0x0
[86429.876111] # GUEST_DS_BASE = 0x0
[86429.876112] # GUEST_FS_BASE = 0x0
[86429.876112] # GUEST_GS_BASE = 0x0
[86429.876113] # GUEST_LDTR_BASE = 0x0
[86429.876114] # GUEST_TR_BASE = 0x0
[86429.876114] # GUEST_GDTR_BASE = 0x0
[86429.876115] # GUEST_IDTR_BASE = 0x0
[86429.876117] # GUEST_DR7 = 0x400
[86429.876118] # GUEST_RSP = 0x7bfa
[86429.876118] # GUEST_RIP = 0x7c00
[86429.876119] # GUEST_RFLAGS = 0x2
[86429.876120] # GUEST_PENDING_DEBUG_EXCEPT = 0x0
[86429.876120] # GUEST_IA32_SYSENTER_ESP = 0x0
[86429.876121] # GUEST_IA32_SYSENTER_EIP = 0x0
答案 8 :(得分:0)
尝试一下。
控制器:
List<CountryModel> countryList = db.countryTable.ToList();
ViewBag.Country = new SelectList(countryList, "Country", "CountryName");
答案 9 :(得分:0)
在您的视图中,将"country"
替换为"countrydrop"
,就像这样...
@Html.DropDownList("countrydrop", (IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")
答案 10 :(得分:0)
您正面临此问题,因为在发布表单时,重新加载下拉列表后无法在viewbag中找到数据。因此,在从db或静态列表中检索数据时,请确保要在get方法中使用的代码,并将该代码也复制粘贴到post verb中。
快乐编码:)
答案 11 :(得分:0)
可能类似于 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"))">