views:

27

answers:

1

I have the following code that was generated using scaffolding and IDJefe is an int in my database, but I want the end users to choose a name from a comboBox.

How could I accomplish this?

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SeguimientoDocente.Area>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    UTEPSA | Editando Area
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Editando Area: <%: Model.Nombre %></h2>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

        <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.TextBoxFor(model => model.IDJefe) %>
                <%: Html.ValidationMessageFor(model => model.IDJefe) %>
            </div>

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

    <% } %>

    <div>
        <%: Html.ActionLink("Volver a Listado General", "Index") %>
    </div>

</asp:Content>

I've tried the following to no avail.

<%: Html.DropDownList(Model.Jefes???? %>

I could do something like this, but creating a new object for a simple thing like this seems a waste.

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

Is there a better way?

A: 

You could take a look at Editor Templates. Here is an example that sounds similar to what you want to do:

http://blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-editor-templates.aspx

Edit: It involves creating a partial view and then using Data Annotations to call that view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%= Html.DropDownList("",new SelectList((string[]) ViewData["Ratings"],Model)) %>
Nick Canzoneri
He's creating a dropdown from static information, not from information fetched from a data source of some kind.
Sergio Tapia
Did you look at the last example? See above edit.
Nick Canzoneri