簡體   English   中英

使用ASP.Net MVC 2腳手架創建編輯表單

[英]Creating an edit form using ASP.Net MVC 2 scaffolding

我有以下使用腳手架生成的代碼,IDJefe是我的數據庫中的int,但我希望最終用戶從comboBox中選擇一個名稱。

我怎么能做到這一點?

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

有沒有更好的辦法?

您可以查看編輯器模板。 這是一個聽起來與您想要的相似的示例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM