开发者

Help me create a dropdownlist of all Jefes in my database!

I have the following table structure:

Help me create a dropdownlist of all Jefes in my database!

I'm trying to populate a combobox of all the Jefes when editing an Area. Meaning I can change who's in charge of an area.

Here is my AreaController.cs code:

public ActionResult Edit(int id)
        {
            Area area = areaRepository.GetArea(id);
            JefeRepository jefe = new JefeRepository();           
            ViewData["Jefes"] = new SelectList(jefe.FindAllJefes(), "Nombre", "Nombre");
            return View(area);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            Area area = areaRepository.GetArea(id);

            try
            {
                UpdateModel(area);
        开发者_运维问答        areaRepository.Save();

                return RedirectToAction("Details", new { id = area.ID });

            }
            catch (Exception)
            {
                foreach (var issue in area.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
                return View(area);
            }
        }

Note that .FindAllJefes() returns an IQueryable<> collection.

Now in my Edit.aspx file I have the following:

<fieldset>
            <legend>Informacion Detallada de Area | <%: Model.Nombre %></legend>

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

            <div class="editor-label">
                <%: Html.LabelFor(model => model.IDJefe) %>
            </div>
            <div class="editor-field">
                <%: Html.DropDownList("IDJefe", (SelectList)ViewData["Jefes"], "(Select Jefe)") %>                
                <%: Html.ValidationMessageFor(model => model.IDJefe) %>
            </div>

            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>

I receive this error:

There is no ViewData item of type 'IEnumerable' that has the key 'IDJefe'.


Per MSDN :

new SelectList(jefe.FindAllJefes(), "Nombre", "Nombre");

jefe.FindAllJefes() should be an IEnumerable when passed to a SelectList ctor. Your argument which should read jefe.FindAllJefes().ToList()


Without seeing how your custom classes are designed, this is a tough one, but just to hazard a guess:

ViewData["Jefes"] = new SelectList(jefe.FindAllJefes(), "Nombre", "Nombre")

I don't know how your SelectList class is setup, but (maybe) you're using "Nombre" as the ID AND the display value (???).

So your select list is looking for a key called "IDJefe", and not finding it, because you used "Nombre" twice.

    <div class="editor-field">
        <%: Html.DropDownList("IDJefe", (SelectList)ViewData["Jefes"], "(Select Jefe)") %>                
        <%: Html.ValidationMessageFor(model => model.IDJefe) %>
    </div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜