我有一个运行良好的 BeginCollectionItem 中继器,但是当我尝试按照 Joe Stevens 博客添加嵌套列表时,它并没有按我预期的方式工作。
我在使用 AspNetCore 时使用了 BeginCollectionItemCore,我从这里获取了代码,因为它说它已经包含来自博客的内置元素:https ://github.com/saad749/BeginCollectionItemCore
我有一个名为 Section 的主要 BeginCollectionItem 和一个名为 SingleLine 的嵌套集合。我希望代码输出类似: Section[long-code-here].SingleLine[another-code-here].SingleLineTextBlock 但是我得到的是 SingleLine[long-code-here].SingleLineTextBlock
我在下面包含了我的代码;
模型:
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; }
}
}
部分观点:
@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 添加新行:
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);
}
});
});
编辑为:
@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>
}
编辑控制器:
public ActionResult GetNewSingleLine(string containerPrefix)
{
ViewData["ContainerPrefix"] = containerPrefix;
return PartialView("SingleLineViewModel", new SingleLineViewModel());
}