开发者

Creating an edit form using ASP.Net MVC 2 scaffolding

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?


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜