简体   繁体   English

MVC BeginCollectionItem 嵌套列表索引不起作用

[英]MVC BeginCollectionItem Nested List Indexes not working

I've got a BeginCollectionItem repeater that is working perfectly, however when I try and follow the Joe Stevens blog to add nested lists it doesn't work in the way I expected.我有一个完美运行的 BeginCollectionItem 中继器,但是当我尝试按照 Joe Stevens 的博客添加嵌套列表时,它无法按我预期的方式工作。

I've used the BeginCollectionItemCore as I'm using AspNetCore, I've taken the code from here as it says it has the built in elements from the blog already in there: https://github.com/saad749/BeginCollectionItemCore我使用了 BeginCollectionItemCore,因为我使用的是 AspNetCore,我从这里获取了代码,因为它说它已经在那里有来自博客的内置元素: https : //github.com/saad749/BeginCollectionItemCore

I have a main BeginCollectionItem called Section and a nested collection called SingleLine.我有一个名为 Section 的主要 BeginCollectionItem 和一个名为 SingleLine 的嵌套集合。 I expected the code to output something like: Section[long-code-here].SingleLine[another-code-here].SingleLineTextBlock however what I get is SingleLine[long-code-here].SingleLineTextBlock我希望代码输出如下内容: Section[long-code-here].SingleLine[another-code-here].SingleLineTextBlock 但是我得到的是 SingleLine[long-code-here].SingleLineTextBlock

I've included my code below;我在下面包含了我的代码;

Model:模型:

namespace project.Models.SetupViewModels
{
    public class SOPTopTemplateBuilderViewModel
    {
        public List<SectionViewModel> Section { get; set; }
    }
    public class SectionViewModel {
        public int SectionId { get; set; }
        public string SectionText { get; set; }
        public string TopTempId { get; set; }
        public List<SingleLineViewModel> SingleLines { get; set; }
    }
}

Partial view:部分观点:

@model project.Models.SetupViewModels.SectionViewModel
@using HtmlHelpers.BeginCollectionItemCore
<div class="new-section form-row">
    @using (Html.BeginCollectionItem("Section"))
    {
    <div class="top-line">
        <div class="col-12 col-md-11">
             @Html.HiddenFor(m => m.SectionId, new { @class="id" })
             @Html.EditorFor(m => m.SectionText, new { @class = "form-control limit-form"})
         </div>
        <div class="col-12 col-md-1">
        </div>
    </div>
    }
    <div class="main-row">
        <div class="buttons-div">
            <button id="add-single-line" type="button" data-containerPrefix="@ViewData["ContainerPrefix"]">Add Single Text</button>
        </div>
    </div>
    <div class="main-row">

    </div>
</div>

Ajax to add new line: Ajax 添加新行:

var form = $('form');
var recipients = $('.main-row');
$("#add-single-line").click(function(){
     $.ajax({
        type: "POST", 
        url: '@Url.Action("GetNewSingleLine")',
        data: { "containerPrefix": recipients.data("containerPrefix") },
        success: function (data) {
            recipients.append(data);  
        }
     });  
});

EditorFor:编者:

@model project.Models.SetupViewModels.SingleLineViewModel
@using HtmlHelpers.BeginCollectionItemCore
@using (Html.BeginCollectionItem("SingleLine"))
{
    <div class="single-line-row">
        @Html.HiddenFor(m => m.SingleLinkTextID, new { @class="id" })
        @Html.TextBoxFor(m => m.SingleLinkTextBlock, new { @class = "form-control limit-form"})
   </div>
}

EditFor Controller:编辑控制器:

public ActionResult GetNewSingleLine(string containerPrefix)
{
   ViewData["ContainerPrefix"] = containerPrefix;
   return PartialView("SingleLineViewModel", new SingleLineViewModel());
}

I struggled with this exact issue for a while until I happened upon this post - which ended up leading me to an answer (thank you!).我在这个确切的问题上挣扎了一段时间,直到我偶然发现了这篇文章——这最终让我找到了答案(谢谢!)。 In the example above, it is storing the prefix in the button.add-single-line but attempting to load it from the div.main-row so you probably are getting 'undefined' as the prefix.在上面的示例中,它将前缀存储在 button.add-single-line 中,但尝试从 div.main-row 加载它,因此您可能将 'undefined' 作为前缀。 By pulling from the same item which cached the value, the code above will do what is intended.通过从缓存该值的同一个项目中提取,上面的代码将执行预期的操作。

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

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