2013-05-08 107 views
0

這是我的第一個程序,我很不確定如何完成我的代碼。我正在嘗試創建一個新的交易,對於供應商來說,會有多種庫存類型。我迷失瞭如何使用「部分視圖」將清單類型添加到列表中。任何有關如何構造代碼的指導都將不勝感激。這裏是我的代碼:MVC 4在局部視圖中添加/更新列表

的ViewModels:

public class InventoryTransactionParent 
    { 
     [Key] 
     public int InventoryTransactionID { get; set; } 

     [ForeignKey("InventoryTransactionType")] 
     [Display(Name = "Transaction Type")] 
     public int InventoryTransactionTypeID { get; set; } 
     public virtual InventoryTransactionType InventoryTransactionType { get; set; } 

     [Display(Name = "Supplier")] 
     [ForeignKey("Supplier")] 
     public int? SupplierID { get; set; } 
     public virtual Supplier Supplier { get; set; } 

     [Display(Name = "Transaction Date (From previous month only)")] 
     [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] 
     public DateTime InventoryTransactionDate { get; set; } 

     [Display(Name = "Receipt/Invoice No.")] 
     public string InventoryTransactionReceipt { get; set; } 

     [Display(Name = "Transaction By")] 
     public string InventoryTransactionBy { get; set; } 

     [Display(Name = "Created On")] 
     public DateTime InventoryTransactionCreatedDateTime { get; set; } 

     [Display(Name = "Created By")] 
     public string InventoryTransactionCreatedBy { get; set; } 

     public bool InventoryTransactionCancelled { get; set; } 
     public int? InventoryTransactionCancelledSourceID { get; set; } 

     public List<InventoryTypeChild> InventoryTypeChilds { get; set; } 
    } 

public class InventoryTypeChild 
    { 
     [ForeignKey("InventoryType")] 
     [Display(Name = "Inventory Type")] 
     public int InventoryTypeID { get; set; } 
     public virtual InventoryType InventoryType { get; set; } 

     [Display(Name = "Quantity")] 
     public decimal InventoryTransactionQuantity { get; set; } 

     [Display(Name = "Price per Item")] 
     public decimal InventoryTransactionPrice { get; set; } 

     [Display(Name = "Remarks (1000 characters)")] 
     [DataType(DataType.MultilineText)] 
     public string InventoryTransactionRemarks { get; set; } 
    } 

查看:

@model Inventory.ViewModels.InventoryTransactionParent 
    @using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 



    <fieldset> 
     <legend>In Transaction</legend> 


     <div class="editor-label"> 
      @Html.LabelFor(model => model.SupplierID, "Supplier") 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("SupplierID", String.Empty) 
      @Html.ValidationMessageFor(model => model.SupplierID) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.InventoryTransactionDate) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.InventoryTransactionDate, "TransactionDate") 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.InventoryTransactionReceipt) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.InventoryTransactionReceipt) 
      @Html.ValidationMessageFor(model => model.InventoryTransactionReceipt) 
     </div> 
     <div id="inventorytypes"> 
      @using (Html.BeginForm()) { 
       <table> 
        <tr> 
         <td>@Html.LabelFor(model => Model.InventoryTypeChilds[0].InventoryTypeID)</td> 
         <td>@Html.LabelFor(model => Model.InventoryTypeChilds[0].InventoryTransactionPrice)</td> 
         <td>@Html.LabelFor(model => Model.InventoryTypeChilds[0].InventoryTransactionQuantity)</td> 
         <td>@Html.LabelFor(model => Model.InventoryTypeChilds[0].InventoryTransactionRemarks)</td> 
         <td></td> 
        </tr> 
       @{ 
        if (Model.InventoryTypeChilds != null) 
        { 
         for (int i = 0; i < Model.InventoryTypeChilds.Count(); i++) 
         { 
          <tr> 
           <td> 
            @Html.DropDownList("InventoryTypeID", String.Empty) 
            @Html.ValidationMessageFor(model => model.InventoryTypeChilds[i].InventoryTypeID) 
           </td> 
           <td> 
            @Html.EditorFor(model => Model.InventoryTypeChilds[i].InventoryTransactionPrice) 
            @Html.ValidationMessageFor(model => Model.InventoryTypeChilds[i].InventoryTransactionPrice) 
           </td> 
           <td> 
            @Html.EditorFor(model => Model.InventoryTypeChilds[i].InventoryTransactionQuantity) 
            @Html.ValidationMessageFor(model => Model.InventoryTypeChilds[i].InventoryTransactionQuantity) 
           </td> 
           <td> 
            @Html.EditorFor(model => Model.InventoryTypeChilds[i].InventoryTransactionRemarks) 
            @Html.ValidationMessageFor(model => Model.InventoryTypeChilds[i].InventoryTransactionRemarks) 
           </td> 
           <td> 
            <input type="submit" value="Add" /> 
           </td> 
          </tr> 
         } 
        } 
       } 
       </table> 
      } 
     </div> 
     <p> 
      <input type="submit" value="In" /> 
     </p> 
    </fieldset> 
} 

控制器(還是很粗糙):

public ActionResult InMultipleTransaction() 
     { 
      ViewBag.InventoryTypeID = new SelectList(db.InventoryTypes, "InventoryTypeID", "InventoryTypeName"); 
      ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName"); 
      InventoryTransactionParent itp = new InventoryTransactionParent(); 
      itp.InventoryTypeChilds = new List<InventoryTypeChild>(); 
      itp.InventoryTypeChilds.Add(new InventoryTypeChild() 
      { 

      }); 

      return View(itp); 
     } 
+0

我甚至在正確的方向? – blurryMVC 2013-05-08 19:00:49

回答

0

你不需要在one-to-many關係中使用Partial ViewsPartial Views是有用的,如果父母和孩子的對象是在同一時間創建,主要發生在one-to-one關係(但即使如此,大多數人使用ViewModels)。但在你的情況下,你必須首先創建父對象,然後添加許多子對象。

This Plural Sight video would be a good way to start. There is a free trial

+0

嗨,我的要求要求我同時創建父母和孩子。因此我需要將列表保存在緩存中,直到它被提交給數據庫。因此,我想像部分視圖 – blurryMVC 2013-05-08 18:50:54

+0

@blurryMVC我認爲你需要重新審視你的應用程序設計要求。我們正在談論無限數量的「子對象」。您不能像「Parent Object」一樣創建它們,至少不是我所知道的。 – Komengem 2013-05-08 19:16:16

+0

IC,謝謝我會試着去看看。儘管我有一些相關的問題,但我試圖僅在List中提交一個成員。但是,控制器不捕獲@ Html.DropDownList(「InventoryTypeID」,String.Empty)值。有什麼我錯過了嗎? – blurryMVC 2013-05-08 19:25:10