开发者

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

开发者 https://www.devze.com 2023-01-09 12:04 出处:网络
I have the following table structure: 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.

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

精彩评论

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

关注公众号