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 Fram开发者_StackOverflow中文版ework, 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.
You can put an IEnumerable<SelectListItem>
in your ViewData[].
var bossDropDown = from boss Area.Boss
select new SelectListItem
{
Value = boss.ID,
Text = boss.Name
};
ViewData["BossDropDown"] = bossDropDown;
and in your View you can call directly your DropDown like this
<%=Html.DropDownList("BossDropDown", (SelectList)ViewData["BossDropDown"]) %>
EDIT:
With your code, try to change this line
ViewData["jefes"] = jefeList;
to this
ViewData["jefes"] = jefeList.AsEnumerable();
In your view, Change this line
<%: Html.DropDownList("IDJefe", (SelectList)ViewData["jefes"]) %>
to this
<%: Html.DropDownList("jefes", (SelectList)ViewData["jefes"]) %>
since, the name you are calling in your dropdownlist should be the Key of your ViewData that has the IEnumerable<SelectListItem>
do this
JefeRepository jefeRepo = new JefeRepository();
var jefes = jefeRepo.FindAll().OrderBy(x => x.Nombre);
List<SelectListItem> jefeList = new List<SelectListItem>();
foreach (var jefe in jefes)
{
jefeList.Add(new SelectListItem()
{
Text = jefe.Nombre,
Value = jefe.ID.ToString()
});
}
ViewData["Jefes"] = jefeList;
then
<div class="editor-label">
<%: Html.LabelFor(model => model.IDJefe) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("Jefes",ViewData["Jefes"] as List<SelectListItem>, "Select a boss ...") %>
<%: Html.ValidationMessageFor(model => model.IDJefe) %>
</div>
your mistake: you cant converst a List to a Selectlist, this doesnt work. but honestly, i'd do it like this:
JefeRepository jefeRepo = new JefeRepository();
var jefes = jefeRepo.FindAll().OrderBy(x => x.Nombre);
ViewData["Jefes"] = new SelectList(jefes,"ID", "Nombre");
then
<div class="editor-label">
<%: Html.LabelFor(model => model.IDJefe) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("Jefes",ViewData["Jefes"] as SelectList, "Select a boss ...") %>
<%: Html.ValidationMessageFor(model => model.IDJefe) %>
</div>
let me know which one works best for ya ;)
精彩评论