查询订单视图模型
public class InquiryOrderViewModel
{
public InquiryOrder InquiryOrder { get; set; }
public List<InquiryOrderDetail> InquiryOrderDetails { get; set; }
}
InquiryOrderIndex 视图和添加项目的脚本
@model eKnittingData.InquiryOrderViewModel
@using (Html.BeginForm("Save", "InquiryOrder"))
{
<div id="editorRows">
@foreach (var item in Model.InquiryOrderDetails)
{
Html.RenderPartial("_DetailEditorRow", item);
}
</div>
@Html.ActionLink("Add another...", null, null, new { id = "addItem" })
<div class="col-md-6"> <input type="submit" value="Save" class="btn btn-success" /> </div>
}
<script>
$('#addItem').click(function (e) {
e.preventDefault();
var isExist = false;
$('.editorRow').each(function () {
if ($(this).children('.class01').val() == 0 || $(this).children('.class02').find("option:selected").text() == "Select") {
isExist = true;
return false;
}
});
if (isExist == false) {
$('.editorRow').each(function () {
$(".editorRow").children().attr("disabled", "disabled");
});
$.ajax({
url: '@Url.Action("BlankEditorRow", "InquiryOrder")',
cache: false,
success: function (data) {
$("#editorRows").append(data);
}
});
}
});
</script>
DetailEditorRow PartialView
@model eKnittingData.InquiryOrderDetail
@using eKnitting.Helpers
@using (Html.BeginCollectionItem("InquiryOrderDetails"))
{
<div class="editorRow">
@Html.DropDownListFor(a => a.ComponentId, (SelectList)ViewBag.CompList, "Select", new { Class = "class02" })
@Html.DropDownListFor(a => a.DesignCodeId, (SelectList)ViewBag.DCodeList, "Select", new { Class = "class03" })
@Html.TextBoxFor(a => a.NoOfParts, new { Class = "class01" })
<a href="#" class="deleteRow">delete</a>
</div>
}
返回 PartialView 的 ActionResult
public ActionResult BlankEditorRow()
{
var objContext = new KnittingdbContext();
ViewBag.CompList = new SelectList(objContext.Components, "ComponentId", "ComponentName");
ViewBag.DCodeList = new SelectList(objContext.DesignCodes, "DesignCodeId", "DesignCodeCode");
return PartialView("_DetailEditorRow", new InquiryOrderDetail());
}
'GET' 的操作结果
var objContext = new KnittingdbContext();
var newIovm = new InquiryOrderViewModel();
var newIo = new InquiryOrder();
//initial item
var newIoD = new List<InquiryOrderDetail>
{
new InquiryOrderDetail()
};
newIovm.InquiryOrder = newIo;
newIovm.InquiryOrderDetails = newIoD;
ViewBag.CompList = new SelectList(objContext.Components, "ComponentId", "ComponentName");
ViewBag.DCodeList = new SelectList(objContext.DesignCodes, "DesignCodeId", "DesignCodeCode");
return View(newIovm);
'POST' 的 ActionResult
public ActionResult Save(InquiryOrderViewModel inquiryOrderViewModel)
{
.................
}
当我单击添加按钮时,我能够动态添加项目。但是对于 PostBack 它只给了我最后附加的项目。我通过在 post ActionResult 上放置一个断点来检查它。我怎样才能获得 PostBack 的整个集合?我哪里做错了?所有帮助表示赞赏。谢谢!