0

查询订单视图模型

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 的整个集合?我哪里做错了?所有帮助表示赞赏。谢谢!

4

1 回答 1

1

您的脚本设置了一个变量var isExist = false;。当您添加一个新项目时,您会检查该值是否为false(如果您已经达到那么远),然后禁用所有现有输入。

禁用的表单控件不会回发,因此您只能获得添加的最后一行的值。

不清楚为什么要禁用它们,但是如果要阻止编辑现有行,则将它们readonly

$(".editorRow").children().prop("readonly", true);
于 2015-11-14T06:55:16.733 回答