views:

50

answers:

2

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.

A: 

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>

rob waminal
Hello, I've updated my post when your were writing your answer. As you can see I think I'm doing the same thing but it gives me an error. Can you please revise the edit, thanks for your time. :)
Serg
It still gives me an error with your update. It seems the key isn't being saved to the ViewData dictionary. I have no idea why.
Serg
I have edited my response.. please check.
rob waminal
Hi Rob, it still isn't working. I'm fairly sure it should work now as the key in the ViewData is "Jefes" and the itemlist was converted using .AsEnumerable(). I have no idea what to do. Edited my question with updated code.
Serg
A: 

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 ;)

Stefanvds