嗨,我正在尝试从单个表单同时插入多条记录,以下是我的模型和视图
我的模型
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>
请提出任何建议
只使用下拉列表而不是简单的下拉列表。
@Html.DropDownListFor(model => model[i].ItemID, (SelectList)ViewBag.ItemID)