使用 ASP.Net MVC 2 脚手架创建编辑表单
我有使用脚手架生成的以下代码,IDJefe 是我数据库中的一个 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);
}
有更好的办法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以查看编辑器模板。这是一个听起来与您想要执行的操作类似的示例:
链接
编辑:
它涉及创建一个部分视图,然后使用数据注释来调用该视图:
You could take a look at Editor Templates. Here is an example that sounds similar to what you want to do:
Link
Edit:
It involves creating a partial view and then using Data Annotations to call that view: