我有一个BeginCollectionItem中继器工作得很好,但是当我试图跟随Joe博客添加嵌套列表时,它的工作方式并不像我所期望的那样。
我使用了BeginCollectionItemCore,因为我使用的是AspNetCore,我从这里提取了代码,因为它已经包含了博客中的内置元素:https://github.com/saad749/BeginCollectionItemCore
我有一个名为节的主BeginCollectionItem和一个名为SingleLine的嵌套集合。我希望代码输出类似于: Sectionlong-code-here.SingleLineanother-code-here.SingleLineTextBlock,但是我得到的是SingleLinelong- code -code。
我在下面列出了我的代码;
模型:
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);
}
});
});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控制器:
public ActionResult GetNewSingleLine(string containerPrefix)
{
ViewData["ContainerPrefix"] = containerPrefix;
return PartialView("SingleLineViewModel", new SingleLineViewModel());
}发布于 2018-09-29 02:48:01
我为这个确切的问题挣扎了一段时间,直到我偶然发现了这篇文章--它最终让我得到了答案(谢谢!)在上面的示例中,它将前缀存储在keyon.add-单行中,但试图从div.main-行加载前缀,因此您可能会得到“未定义”作为前缀。通过从缓存值的同一项中提取,上面的代码将执行预期的操作。
https://stackoverflow.com/questions/48831878
复制相似问题