Asp.net mvc 模型绑定到复杂对象

Asp.net mvc 模型绑定到复杂对象,asp.net-mvc,razor,drop-down-menu,model-binding,multiple-records,Asp.net Mvc,Razor,Drop Down Menu,Model Binding,Multiple Records,嗨,我正在尝试从一个表单同时插入多个记录,下面是我的模型和视图 我的车型 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&l

嗨,我正在尝试从一个表单同时插入多个记录,下面是我的模型和视图

我的车型

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; }        
    }
公共类发票
{
public int InvoiceID{get;set;}
公共字符串InvoiceNo{get;set;}
公共日期时间发票日期{get;set;}
公共bool发布{get;set;}
公共虚拟列表InvoiceDetails{get;set;}
}
公共类发票明细
{
public int InvoiceDetailID{get;set;}
public int InvoiceID{get;set;}
公共发票{get;set;}
公共int ItemID{get;set;}
公共虚拟项项{get;set;}
公共双价{get;set;}
公共双数量{get;set;}
}
公共类项目
{
公共int ItemID{get;set;}
公共字符串说明{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>
}
@model-IList
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
发票明细
@对于(int i=0;i<2;i++)
{        
@LabelFor(model=>model[i].ItemID,“Item”)
@{string itemid=“[”+i+“].itemid”;
@DropDownList(“ItemID”,String.Empty)
@Html.ValidationMessageFor(model=>model[i].ItemID)
}
@LabelFor(model=>model[i].Price)
@EditorFor(model=>model[i].Price)
@Html.ValidationMessageFor(model=>model[i].Price)
@LabelFor(model=>model[i].Quantity)
@Html.EditorFor(model=>model[i].Quantity)
@Html.ValidationMessageFor(model=>model[i].Quantity)
}

}
问题是我的代码生成如下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>


任何建议请使用dropdownlist for而不是简单的dropdownlist

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