gpt4 book ai didi

c# - 带有 List<> 项目的玻璃映射器 Editable() 不工作?

转载 作者:太空狗 更新时间:2023-10-30 00:30:33 26 4
gpt4 key购买 nike

我有一个从 sitecore 读取并在我的 View 中循环的项目列表。这些项目非常简单,简单地输出为一系列 <li>

型号

[SitecoreType(AutoMap = true)]
public class Model
{
[SitecoreId]
public Guid Id { get; set; }
public string Name { get; set; }

public string DisplayName { get; set; }

public string Path { get; set; }
[SitecoreField]
public virtual string TabText { get; set; }
}

我在a中使用上面的模型

View 模型

public class ViewModel
{
public List<Model> Models {get; set;}
}

虽然我正在努力在我的 View 中使用这个 View 模型并使用 glassmappers Editable特点:

查看

@using Sitecore.Mvc
@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<ViewModel>
<ul>
@for (int specCounter = 0; specCounter < Model.Models.Count; specCounter++)
{
<!--@specCounter-->
<li>
@Editable(e => e.Models[specCounter].TabText)</li>
}
</ul>

这会导致一系列错误被写入结果 HTML:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) at lambda_method(Closure , SpecificationInDetailViewModel ) at Glass.Mapper.Sc.GlassHtml.MakeEditable[T](Expression1 field,
Expression
1 standardOutput, T model, Object parameters, Context context, Database database, TextWriter writer) Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

如果我删除 Editable然后调用<li>正确输出。如果我使用 foreach循环(与 for 循环相对):

@using Sitecore.Mvc
@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<ViewModel>
<ul>
@foreach (var model in Model.Models)
{
<!--@specCounter-->
<li>
@Editable(e => model.TabText)</li>
}
</ul>

然后所有 <li>包含来自最后一个模型的文本(显然是某种传递引用问题)。再次删除 Editable产生正确的结果。

有什么解决办法吗?我希望这些项目可以在体验查看器中进行编辑。

最佳答案

您应该将目标 Glass Item 作为第一个参数传递给 Editable:

@for (int specCounter = 0; specCounter < Model.Models.Count; specCounter++)
{
<!--@specCounter-->
<li>
@Editable(Model.Models[specCounter], e => e.TabText)</li>
}

foreach:

@foreach (var model in Model.Models)
{
<!--@specCounter-->
<li>
@Editable(model, e => e.TabText)</li>
}

更多信息,查看Glass Tutotial - MVC Page Editor Support .

关于c# - 带有 List<> 项目的玻璃映射器 Editable() 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35011145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com