ASP.NET MVC 2 - 在表单提交上保存子实体

时间:2010-06-12 18:53:02

标签: asp.net asp.net-mvc

我正在使用ASP.NET MVC 2并且正在努力保存子实体。我有一个现有的Invoice实体(我在一个单独的表单上创建),然后我有一个LogHours视图,我想用来保存InvoiceLog,它是Invoice的子实体。以下是观点:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TothSolutions.Data.Invoice>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Log Hours
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript">
        $(document).ready(function () {
            $("#InvoiceLogs_0__Description").focus();
        });
    </script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Log Hours</h2>

<% using (Html.BeginForm("SaveHours", "Invoices")) {%>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>Fields</legend>

        <table>
            <tr>
                <th>Date</th>
                <th>Description</th>
                <th>Hours</th>
            </tr>
            <%
           int index = 0;
           foreach (var log in Model.InvoiceLogs) { 
            %>
                <tr>
                    <td><%: log.LogDate.ToShortDateString() %></td>
                    <td><%: Html.TextBox("InvoiceLogs[" + index + "].Description")%></td>
                    <td><%: Html.TextBox("InvoiceLogs[" + index + "].Hours")%></td>
                    <td>Hours</td>
                </tr>
            <% 
               index++;
           } 
           %>
        </table>

        <p>
            <%: Html.Hidden("InvoiceID") %>
            <%: Html.Hidden("CreateDate") %>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
 <% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

</asp:Content>

这是控制器代码:

//GET: /Secure/Invoices/LogHours/
        public ActionResult LogHours(int id)
        {
            var invoice = DataContext.InvoiceData.Get(id);
            if (invoice == null)
            {
                throw new Exception("Invoice not found with id: " + id);
            }
            return View(invoice);
        }

//POST: /Secure/Invoices/SaveHours/
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SaveHours([Bind(Exclude = "InvoiceLogs")]Invoice invoice)
        {
            TryUpdateModel(invoice.InvoiceLogs, "InvoiceLogs");
            invoice.UpdateDate = DateTime.Now;
            invoice.DeveloperID = DeveloperID;
            //attaching existing invoice.
            DataContext.InvoiceData.Attach(invoice);
            //save changes.
            DataContext.SaveChanges();
            //redirect to invoice list.
            return RedirectToAction("Index");
        }

数据访问代码:

public static void Attach(Invoice invoice)
            {
                var i = new Invoice { InvoiceID = invoice.InvoiceID };
                db.Invoices.Attach(i);
                db.Invoices.ApplyCurrentValues(invoice);
            }

在SaveHours操作中,它在我调用TryUpdateModel后正确设置InvoiceLog实体的值,但是当它执行SaveChanges时,它不会使用新值更新数据库。此外,如果您手动更新数据库中的InvoiceLog条目的值,然后转到此页面,它不会填充文本框,因此它显然没有正确绑定。

谢谢, 贾斯汀

1 个答案:

答案 0 :(得分:0)

搞定了,我需要在隐藏字段中填充InvoiceLogID和InvoiceID,以便在EntityCollection中填充它们。