2010-07-28 78 views
3

我有以下使用脚手架生成的代码,IDJefe是我的数据库中的一个int,但我希望最终用户从comboBox中选择一个名称。使用ASP.Net创建编辑窗体MVC 2脚手架

我怎么能做到这一点?

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

有没有更好的方法?

回答

0

你可以看看编辑模板。这里是你想要做什么这听起来类似的例子:

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

他创造从静态信息下拉,而不是从某种数据源获取的信息。 – 2010-07-28 15:34:05

+1

你看过最后一个例子吗?见上面的编辑。 – 2010-07-28 15:44:23

相关问题