3

スキャフォールディングを使用して生成された次のコードがあり、IDFefe はデータベース内の int ですが、エンド ユーザーにコンボ ボックスから名前を選択してもらいたいと考えています。

どうすればこれを達成できますか?

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

私は無駄に次のことを試しました。

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

このようなこともできますが、このような単純なことのために新しいオブジェクトを作成するのは無駄に思えます。

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

より良い方法はありますか?

4

1 に答える 1

0

Editor Templates を参照してください。これは、あなたがやりたいことと似ているように聞こえる例です:

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

編集:部分ビューを作成し、データ注釈を使用してそのビューを呼び出す必要があります。

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%= Html.DropDownList("",new SelectList((string[]) ViewData["Ratings"],Model)) %>
于 2010-07-28T15:32:05.600 に答える