0

こんにちは、単一のフォームから同時に複数のレコードを挿入しようとしています。以下は私のモデルとビューです

マイモデル

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>

何か提案をしてください

4

1 に答える 1

0

単純なドロップダウンリストではなく、ドロップダウンリストを使用してください。

@Html.DropDownListFor(model => model[i].ItemID, (SelectList)ViewBag.ItemID)
于 2012-11-17T14:16:24.740 に答える