简体   繁体   English

BeginCollectionItem()仅为PostBack提供最后附加的项目

[英]BeginCollectionItem() gives only lastly appended item for PostBack

InquiryOrderViewModel InquiryOrderViewModel

public class InquiryOrderViewModel
{
    public InquiryOrder InquiryOrder { get; set; }
    public List<InquiryOrderDetail> InquiryOrderDetails { get; set; }
}

InquiryOrderIndex View and the Script to add items 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 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>
}

ActionResult which returns PartialView 返回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 'GET' “ 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);

ActionResult for 'POST' “ POST”的ActionResult

public ActionResult Save(InquiryOrderViewModel inquiryOrderViewModel)
{
     .................
}

When i click the add button im able to add items dynamically. 当我单击添加按钮时,我无法动态添加项目。 But for PostBack it gives me only the lastly appended item. 但是对于PostBack,它只给我最后附加的项目。 I checked it by putting a break point on post ActionResult. 我通过在ActionResult上放置一个断点来检查它。 How can i get the whole collection for PostBack? 如何获得PostBack的整个收藏集? Where did i go wrong? 我哪里做错了? All help appreciated. 所有帮助表示赞赏。 Thanks! 谢谢!

Your scripts sets a variable var isExist = false; 您的脚本设置了一个变量var isExist = false; . When you add a new item, you check if the value is false (which it is if you got that far) and then disable all existing inputs. 添加新项时,请检查该值是否为false (如果到此距离,则为false ),然后禁用所有现有输入。

Disabled form controls do not post back, hence you only get the values for the last row you have added. 禁用的表单控件不会回发,因此您只能获取添加的最后一行的值。

Its unclear why you would want to disable them, but if you want to prevent editing of existing rows, the make them readonly 目前尚不清楚为什么要禁用它们,但是如果要防止编辑现有行,请将其设为readonly

$(".editorRow").children().prop("readonly", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM