使用 ASP.Net MVC 2 脚手架创建编辑表单

发布于 2024-09-11 14:48:52 字数 2230 浏览 0 评论 0原文

我有使用脚手架生成的以下代码,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2024-09-18 14:48:52

您可以查看编辑器模板。这是一个听起来与您想要执行的操作类似的示例:

链接

编辑:
它涉及创建一个部分视图,然后使用数据注释来调用该视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%= Html.DropDownList("",new SelectList((string[]) ViewData["Ratings"],Model)) %>

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:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%= Html.DropDownList("",new SelectList((string[]) ViewData["Ratings"],Model)) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文