问题描述
有一对夫妇在Stack Overflow上这个职位,但没有一个答案,似乎修复我目前的状况的问题。
There are a couple of posts about this on Stack Overflow but none with an answer that seem to fix the problem in my current situation.
我在这一个表的页面时,每行有一些文本字段和一个下拉的。所有下拉菜单需要使用相同的SelectList的数据,所以我已经将它设置如下:
I have a page with a table in it, each row has a number of text fields and a dropdown. All the dropdowns need to use the same SelectList data so I have set it up as follows:
控制器
ViewData["Submarkets"] = new SelectList(submarketRep.AllOrdered(), "id", "name");
查看
<%= Html.DropDownList("submarket_0", (SelectList)ViewData["Submarkets"], "(none)") %>
我用的正是这种建立在很多地方,但由于某种原因在这期特别我得到的错误:
I have used exactly this setup in many places, but for some reason in this particular view I get the error:
有型'的IEnumerable具有关键submarket_0没有ViewData的项目。
推荐答案
好了,答案是从对这个问题的一些其他职位派生,它是:
Ok, so the answer was derived from some other posts about this problem and it is:
如果你的的ViewData code>包含
的SelectList
使用相同的名称的DropDownList
即submarket_0时,HTML辅助会自动填充您的的DropDownList
与该数据如果您不指定第二个参数在这种情况下是源的SelectList
If your ViewData
contains a SelectList
with the same name as your DropDownList
i.e. "submarket_0", the Html helper will automatically populate your DropDownList
with that data if you don't specify the 2nd parameter which in this case is the source SelectList.
发生了什么事与我的错误是:
What happened with my error was:
由于含有下拉列表表是在一个局部视图和的ViewData code>已被更改,不再包含在
的SelectList
我曾引用了的HtmlHelper
(而不是抛出一个错误)试图找到所谓的submarket_0选择列表中的ViewData(GRRRR!)它STILL不可能发现,然后就抛出一个错误:)
Because the table containing the drop down lists was in a partial view and the ViewData
had been changed and no longer contained the SelectList
I had referenced, the HtmlHelper
(instead of throwing an error) tried to find the SelectList called "submarket_0" in the ViewData (GRRRR!!!) which it STILL couldnt find, and then threw an error on that :)
请纠正我,如果我错了
这篇关于有型无ViewData的项目'的IEnumerable&LT; SelectListItem&GT;'这有钥匙'XXX'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!