Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

MVC BeginCollectionItem Nested List Indexes not working

$
0
0

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.

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

I have a main BeginCollectionItem called Section and a nested collection called 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

I've included my code below:

Model:

namespace project.Models.SetupViewModels{publicclassSOPTopTemplateBuilderViewModel{publicList<SectionViewModel>Section{get;set;}}publicclassSectionViewModel{publicintSectionId{get;set;}publicstringSectionText{get;set;}publicstringTopTempId{get;set;}publicList<SingleLineViewModel>SingleLines{get;set;}}}

Partial view:

@model sopman.Models.SetupViewModels.SectionViewModel@usingHtmlHelpers.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"]">AddSingleText</button></div></div><div class="main-row"></div></div>

Ajax to add new line:

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 sopman.Models.SetupViewModels.SingleLineViewModel@usingHtmlHelpers.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:

publicActionResultGetNewSingleLine(string containerPrefix){ViewData["ContainerPrefix"]= containerPrefix;returnPartialView("SingleLineViewModel",newSingleLineViewModel());}

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>