2017-09-21 32 views
0

我設計的樣品發票enter image description here,和我的課像發票使用Asp.net MVC

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類發票,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)); 
    } 

回答

0

,如果你使用invoiceDetails爲對象的名單,你應該重寫功能集在數據庫

+0

插入如何做到這一點... ...請寫代碼??? –

+0

你的控制器發佈方法是什麼樣的?你能舉一個例子嗎? –

+0

請在查看代碼後添加它... –