InquiryOrderViewModel
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());
}
ActionResult for&#39; GET&#39;
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);
&#39; POST&#39;
的ActionResultpublic ActionResult Save(InquiryOrderViewModel inquiryOrderViewModel)
{
.................
}
当我点击添加按钮时,我可以动态添加项目。但是对于PostBack,它只给我最后添加的项目。我通过在帖子ActionResult上放置一个断点来检查它。我如何获得PostBack的整个系列?我哪里做错了?所有帮助赞赏。谢谢!
答案 0 :(得分:1)
您的脚本设置变量var isExist = false;
。当您添加新项目时,检查该值是否为false
(如果您已达到该值,则为该值),然后禁用所有现有输入。
禁用的表单控件不会回发,因此您只能获取已添加的最后一行的值。
目前还不清楚为什么要禁用它们,但是如果要阻止编辑现有行,请将它们设为readonly
$(".editorRow").children().prop("readonly", true);