如何创建 DropDownList?
我有两个表:Area 和 Boss。
一个Area有一个Boss,它有一个外键关系Area.IDBoss,即Boss.ID。我希望我能正确解释我的问题。 :P
截至目前,我可以在文本框中手动输入数字并正确保存,我还可以正确显示名称,因为我正在使用实体框架,例如视图中的“item.Boss.ID”并且它可以工作美好的。
不过,我确实需要显示一个 DropDownList。
我猜我必须使用 ViewData[] 字典从数据库返回可用“Boss”行的集合,但老实说我被困住了。 :D
有什么建议吗?这是一个非常简单的用例,所以希望我不会在上面花费太多时间。如果你们需要我的任何代码,请直接说,但我怀疑这对解决如此简单的问题有帮助。
一如既往,这个网站非常棒,感谢您的帮助。
编辑: 也许发布一些代码会有帮助。现在我收到此错误:
编辑2: 使用最新的代码进行编辑,但仍无法正常工作。
没有 ViewData 类型的项 'IEnumerable' 具有 关键“Jefes”。
这是代码:
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.IDJefe) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("Jefes", (SelectList)ViewData["Jefes"]) %>
<%--<%: Html.TextBoxFor(model => model.IDJefe) %>--%>
<%: Html.ValidationMessageFor(model => model.IDJefe) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Nombre) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Nombre) %>
<%: Html.ValidationMessageFor(model => model.Nombre) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
JefeRepository jefeRepo = new JefeRepository();
var jefes = jefeRepo.FindAll().OrderBy(x => x.Nombre);
var jefeList = new List<SelectListItem>();
foreach (var jefe in jefes)
{
jefeList.Add(new SelectListItem()
{
Text = jefe.Nombre,
Value = jefe.ID.ToString()
});
}
ViewData["Jefes"] = jefeList.AsEnumerable();
顺便说一下,我将一些变量名称从西班牙语翻译成英语,这样它们会更有意义,对于造成的混乱表示抱歉。
编辑3: 如果你们需要更多信息,请告诉我。我已经检查了每一行代码,但它不起作用。
I have two tables: Area and Boss.
An Area has a Boss, it has a foreign key relationship of Area.IDBoss and that is Boss.ID. I hope I'm explaining my question properly. :P
As of now I can manually type in a number in the textbox and it saves correctly, I can also display the name correctly because I'm using Entity Framework, something like "item.Boss.ID" in the View and it works fine.
I really need to display a DropDownList though.
I'm guessing I have to return a collection of available "Boss" rows from my database using the ViewData[] dictionary, but I'm honestly stuck. :D
Any suggestions? This is a very simple use case, so hopefully I don't spend too much time on it. If you guys need any code from my part just say, but I doubt it would help for such a simple question.
As always this site is fantastic, thanks for the help.
Edit:
Maybe posting some code would help. Right now I'm receiving this error:
Edit 2:
Edited with more recent code that is still not working.
There is no ViewData item of type
'IEnumerable' that has
the key 'Jefes'.
And here's the code:
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.IDJefe) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("Jefes", (SelectList)ViewData["Jefes"]) %>
<%--<%: Html.TextBoxFor(model => model.IDJefe) %>--%>
<%: Html.ValidationMessageFor(model => model.IDJefe) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Nombre) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Nombre) %>
<%: Html.ValidationMessageFor(model => model.Nombre) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
JefeRepository jefeRepo = new JefeRepository();
var jefes = jefeRepo.FindAll().OrderBy(x => x.Nombre);
var jefeList = new List<SelectListItem>();
foreach (var jefe in jefes)
{
jefeList.Add(new SelectListItem()
{
Text = jefe.Nombre,
Value = jefe.ID.ToString()
});
}
ViewData["Jefes"] = jefeList.AsEnumerable();
By the way I translated some variable names from Spanish to English so they would make more sense, sorry for the confusion.
Edit 3:
If you guys need any more information please let me know. I've looked over every line of code but it just doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
IEnumerable
放入 ViewData[] 中。在您的视图中,您可以像这样直接调用您的下拉菜单
编辑:
使用您的代码,尝试将此行更改
为此
在您的视图中,将此行更改
为此
,因为您在下拉列表中调用的名称应该是您的键具有
IEnumerable
的 ViewDataYou can put an
IEnumerable<SelectListItem>
in your ViewData[].and in your View you can call directly your DropDown like this
EDIT:
With your code, try to change this line
to this
In your view, Change this line
to this
since, the name you are calling in your dropdownlist should be the Key of your ViewData that has the
IEnumerable<SelectListItem>
这样做
然后
你的错误:你不能将列表转换为选择列表,这不起作用。
但老实说,我会这样做:
然后
让我知道哪一个最适合你;)
do this
then
your mistake: you cant converst a List to a Selectlist, this doesnt work.
but honestly, i'd do it like this:
then
let me know which one works best for ya ;)