0

你好我想插入從單一的形式同時多條記錄以下是我的模型和視圖ModelBinding將複雜的對象

我的模型

public class Invoice 
{ 
    public int InvoiceID { get; set; } 
    public string InvoiceNo { get; set; } 
    public DateTime InvoiceDate { get; set; } 
    public bool Posted { get; set; } 

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


public class InvoiceDetail 
    { 
     public int InvoiceDetailID { get; set; } 

     public int InvoiceID { get; set; } 
     public Invoice Invoice { get; set; } 

     public int ItemID { get; set; } 
     public virtual Item Item { get; set; } 

     public double Price { get; set; } 
     public double Quantity { get; set; } 
    } 

public class Item 
    { 
     public int ItemID { get; set; } 
     public string Description { get; set; }   
    } 

在查看

@model IList<MultiInsert.Models.InvoiceDetail> 
@{ 
    ViewBag.Title = "Create"; 
} 
<h2> 
    Create</h2> 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>InvoiceDetail</legend> 
     @for (int i = 0; i < 2; i++) 
     {   
      <div class="editor-label"> 
       @Html.LabelFor(model => model[i].ItemID, "Item") 
      </div> 
      <div class="editor-field"> 
       @{string itemid = "[" + i + "].ItemID"; 
        @Html.DropDownList("ItemID", String.Empty) 
        @Html.ValidationMessageFor(model => model[i].ItemID) 
       } 
      </div> 

      <div class="editor-label"> 
       @Html.LabelFor(model => model[i].Price) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model[i].Price) 
       @Html.ValidationMessageFor(model => model[i].Price) 
      </div> 

      <div class="editor-label"> 
       @Html.LabelFor(model => model[i].Quantity) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model[i].Quantity) 
       @Html.ValidationMessageFor(model => model[i].Quantity) 
      </div> 
     } 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

問題是我的代碼生成如下的html,如下所示

<div class="editor-field"> 
<select id="ItemID" name="ItemID"><option value=""></option> 
<option value="1">SALT</option> 
<option value="2">RED CHILI</option> 
</select><span class="field-validation-valid" data-valmsg-for="[0].ItemID" data-valmsg-replace="true"></span>   </div> 
      <div class="editor-label"> 
       <label for="">Price</label> 
      </div> 
      <div class="editor-field"> 
       <input class="text-box single-line" data-val="true" data-val-number="The field Price must be a number." data-val-required="The Price field is required." name="[0].Price" type="text" value="" /> 
       <span class="field-validation-valid" data-valmsg-for="[0].Price" data-valmsg-replace="true"></span> 
      </div> 
      <div class="editor-label"> 
       <label for="">Quantity</label> 
      </div> 
      <div class="editor-field"> 
       <input class="text-box single-line" data-val="true" data-val-number="The field Quantity must be a number." data-val-required="The Quantity field is required." name="[0].Quantity" type="text" value="" /> 
       <span class="field-validation-valid" data-valmsg-for="[0].Quantity" data-valmsg-replace="true"></span> 
      </div> 

而應該產生類似

<select id="[0].ItemID" name="[1].ItemID"><option value=""></option> 

任何建議請

+0

@ Html.DropDownListFor(model => model [i] .ItemID,(SelectList)ViewBag.ItemID) – Awan

回答

0

只使用dropdownlistfor而不是簡單的下拉列表。

@Html.DropDownListFor(model => model[i].ItemID, (SelectList)ViewBag.ItemID)