使用Asp.net MVC发票

时间:2017-09-21 00:40:41

标签: jquery asp.net-mvc oop

我设计了样本发票enter image description here,我的课程就像那样

public partial class Invoice
{
    public Invoice()
    {
        this.InvoiceDetails = new HashSet<InvoiceDetail>();
    }

    public int code { get; set; }
    public Nullable<System.DateTime> inv_date { get; set; }
    public Nullable<decimal> total { get; set; }
    public Nullable<decimal> discount { get; set; }
    public Nullable<decimal> net { get; set; }

    public virtual ICollection<InvoiceDetail> InvoiceDetails { get; set; }
}

public partial class InvoiceDetails
{
    public int id { get; set; }
    public int code { get; set; }
    public Nullable<int> itemId { get; set; }
    public string itemName { get; set; }
    public Nullable<decimal> price { get; set; }
    public Nullable<int> request_quantity { get; set; }
    public Nullable<decimal> total { get; set; }
    public Nullable<int> vendorId { get; set; }

    public virtual Invoice Invoice { get; set; }
    public virtual Item Item { get; set; }
}

public partial class Item
{
    public Item()
    {
        this.InvoiceDetails = new HashSet<InvoiceDetail>();
    }
    public int id { get; set; }
    public string name { get; set; }
    public Nullable<decimal> price { get; set; }
    public Nullable<int> quantity { get; set; }
    public Nullable<int> vendorId { get; set; }

    public virtual ICollection<InvoiceDetail> InvoiceDetails { get; set; }
    public virtual Vendor Vendor { get; set; }
}

    public partial class Vendor
{
    public Vendor()
    {
        this.Items = new HashSet<Item>();
    }

    public int id { get; set; }
    public string name { get; set; }

    public virtual ICollection<Item> Items { get; set; }
}

最后,课程正在收集3个班级Invoice,InvoiceDetails和供应商

public class Collection_invoice
{
    public Invoice Invoice { get; set; }
    public InvoiceDetail InvoiceDetail { get; set; }
    public IEnumerable<Vendor> Vendor { get; set; }
}

我在视图模型中使用此类来创建新发票并且一切都很好,但如果发票有两个或更多项,那么只保存最终项目就像那样添加

Only last item will be insert in database ... how to solve that ??

我的问题是在SQL服务器中保存发票内的所有项目所需的更改...请帮助..

这是创建新发票的视图模型(最后一张图片)

@model PurchasesInvoice.Models.Collection_invoice
@using (Html.BeginForm())
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="form-horizontal">
                <div class="col-md-4">
                    <div class="form-group">
                        @Html.LabelFor(m => m.Invoice.code)
                        @Html.TextBoxFor(m => m.Invoice.code, new { @readonly = "readonly", @class = "form-control" })
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        @Html.LabelFor(m => m)
                        <br />
                        @Html.DropDownListFor(m=>m.Invoice.InvoiceDetails,
                        new SelectList(Model.Vendor, "id", "name"), "Select Vendor", new { @class = "dropdown-toggle btn btn-primary", @onchange = "Get_Items()", @required = "required" })
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        @Html.Label("Invoice Date")
                        <br />
                        @Html.EditorFor(m => m.Invoice.inv_date, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Invoice.inv_date, " ", new { @class = "req" })
                    </div>
                </div>

<table class="direction table table-bordered">
                    <thead>
                        <tr>
                            <th>
                                @Html.DisplayName("Item Id")
                            </th>
                            <th>
                                @Html.DisplayName("Item Name")
                            </th>
                            <th>
                                @Html.DisplayName("Price")
                            </th>
                            <th>
                                @Html.DisplayName("Quantity")
                            </th>
                            <th>
                                @Html.DisplayName("Request quantity")
                            </th>
                            <th>
                                @Html.DisplayName("Total")
                            </th>
                            <th>
                                @Html.DisplayName("Action")

                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                                <div class="col-lg-8">
                                    <div class="form-group">
                                        @Html.TextBoxFor(m => m.InvoiceDetail.itemId, new { @readonly = "readonly", @id = "item_Id", @class = "form-control" })
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="col-lg-4">
                                    <div class="form-group">
                                        @Html.DropDownList("itemId", new List<SelectListItem>{
                                        new SelectListItem{Text ="-- Select Item --", Value=" "} },
                                            "-- Select Item --", new { @class = "dropdown-toggle btn btn-primary", @required = "required", @onchange = "Get_dataItem()" })
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="col-lg-8">
                                    <div class="form-group">
                                        @Html.TextBoxFor(m => m.InvoiceDetail.price, new { @id = "price", @readonly = "readonly", @class = "form-control" })
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="col-lg-8">
                                    <div class="form-group">
                                        @Html.TextBox("quantity", " ", new { @id = "quantity", @class = "form-control", @readonly = "readonly" })
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="col-lg-8">
                                    <div class="form-group">
                                        @Html.TextBoxFor(m => m.InvoiceDetail.request_quantity, " ", new { @id = "request_quantity", @onchange = "calculateQuantity()", @class = "form-control" })
                                        @Html.ValidationMessageFor(m => m.InvoiceDetail.request_quantity, " ", new { @class = "req" })
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="col-lg-8">
                                    <div class="form-group">
                                        @Html.TextBoxFor(m => m.InvoiceDetail.total, new { @id = "total", @readonly = "readonly", @class = "label-info form-control" })
                                    </div>
                                </div>
                            </td>
                            <td>
                                <button id="btnAddItem" type="button" class="btn btn-primary">Add Item</button>
                            </td>
                        </tr>
                    </tbody>
                </table>
<table id="table-items" class="direction table table-bordered table-condensed">
                    <thead>
                        <tr>
                            <th>
                                @Html.DisplayName("Item Id")
                            </th>
                            <th>
                                @Html.DisplayName("Item Name")
                            </th>
                            <th>
                                @Html.DisplayName("Price")
                            </th>
                            <th>
                                @Html.DisplayName("Quantity")
                            </th>
                            <th>
                                @Html.DisplayName("Total")
                            </th>
                    </thead>
                    <tbody>
                        <tr></tr>
                    </tbody>
                </table>
                <div class="col-md-3">
                    <div class="form-group">
                        @Html.LabelFor(m => m.Invoice.total)
                        @Html.TextBoxFor(m => m.Invoice.total, new { @readonly = "readonly", @class = "form-control" })
                    </div>
                </div>
                <div class="col-md-1">
                    <div class="form-group">
                        @Html.LabelFor(m => m.Invoice.discount)
                        <div class="form-horizontal">
                            <div class="form-group">
                                @Html.TextBoxFor(m => m.Invoice.discount, new { @onchange = "CalculateNetInvoice()", @class = "form-control" })<span>%</span>
                            </div>
                        </div>
                        @Html.ValidationMessageFor(m => m.Invoice.discount, "", new { @class = "req" })
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        @Html.LabelFor(m => m.Invoice.net)
                        @Html.TextBoxFor(m => m.Invoice.net, new { @readonly = "readonly", @class = "form-control" })
                    </div>
                </div>
                <div class="col-md-3">
                    <br />
                    <input class="btn btn-primary" type="submit" id="btnInsert" name="btnInsert" value="Save Invoice" />
                </div>
            </div>
        </div>
    </div>
</div>



        [HttpPost]
    public ActionResult Create(Collection_invoice coInvoice)
    {
        if (coInvoice.Invoice != null)
        {
            Invoice inv = new Invoice();
            inv.inv_date = coInvoice.Invoice.inv_date;
            inv.net = coInvoice.Invoice.net;
            inv.total = coInvoice.Invoice.total;
            inv.discount = coInvoice.Invoice.discount;

            int code = inv.AddNewInvoice(inv);

            coInvoice.InvoiceDetail.code = code;
            coInvoice.InvoiceDetail.itemName = db.Items.SingleOrDefault(im => im.id == coInvoice.InvoiceDetail.itemId).name;
            db.InvoiceDetails.Add(coInvoice.InvoiceDetail);
            db.SaveChanges();
        }
        else
        {
            var VendorList = db.Vendors.ToList();
            var invoice_detalis = new Models.Collection_invoice
            {
                Vendor = VendorList
            };
            return View("Create", invoice_detalis);
        }
        int? page = null;
        return View("List", db.Invoices.ToList().ToPagedList(page ?? 1, 3));
    }

1 个答案:

答案 0 :(得分:0)

如果您使用invoiceDetails作为对象列表,则应覆盖要在数据库中插入的set函数