我有一个项目列表,我正在读取sitecore并在我的视图中循环。这些项目非常直接,只是作为一系列
输出
型号:
[SitecoreType(TemplateId = "{1044CFB5-2B85-4A8D-9DCC-34764D2AF5B3}", AutoMap = true)]
public class Footer
{
public virtual Item Item { get; set; }
[SitecoreInfo(SitecoreInfoType.Path)]
public virtual string Path { get; set; }
[SitecoreField(FieldName ="Copyright Text First",FieldType = SitecoreFieldType.SingleLineText)]
public virtual string CopyrightTextFirst { get; set; }
[SitecoreField(FieldName ="Copyright Text Last",FieldType = SitecoreFieldType.SingleLineText)]
public virtual string CopyrightTextLast { get; set; }
public IEnumerable<BrandNavigaion> lstBrandNav {get;set; }
}
[SitecoreType(TemplateId = "{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}", AutoMap = true)]
public class BrandNavigaion
{
public virtual Item Item { get; set; }
[SitecoreInfo(SitecoreInfoType.Path)]
public virtual string Path { get; set; }
//[SitecoreField(FieldName = "Item name")]
public virtual string Itemname { get; set; }
[SitecoreField(FieldName = "Destination URL",FieldType=SitecoreFieldType.GeneralLink)]
public virtual string DestinationURL { get; set; }
[SitecoreField(FieldName = "Is Active ", FieldType = SitecoreFieldType.Checkbox)]
public virtual bool IsActive { get; set; }
public virtual IEnumerable<Sitecore.Data.Items.Item> Children { get; set; }
//public virtual IEnumerable<BrandSubNavigaion> Children { get; set; }
}
查看
@foreach (var item in Model.lstBrandNav)
{
<li>
<a href="@item.DestinationURL">
@Editable(item, i => i.Itemname)
</a>
</li>
if (item.Children != null && item.Children.Count() > 0)
{
<li class="">
<a data-toggle="dropdown">Associate Sites <i class="fa fa-angle-down"></i></a>
<ul>
@foreach (var childitem in item.Children)
{
<li><a href="">@childitem.Name</a></li>
}
</ul>
</li>
}
}
但是在编辑模式下显示如下
请帮助我,我的代码有什么问题。
答案 0 :(得分:0)
首先,您不需要有2个字段DestinationUrl和ItemName。 你也可以在DestionationUrl上拥有ItemName的值。 DestinationUrl是一个通用链接字段,它包含url和文本字段。
您可以为常规链接字段提供多个属性:
您可以更改代码吗
SitecoreField(FieldName = "Destination URL",FieldType=SitecoreFieldType.GeneralLink)]
public virtual Glass.Mapper.Sc.Fields.Link DestinationURL { get; set; }
以这种方式呈现您的链接:
@using (BeginRenderLink(item, x => x.DestinationUrl, new { @class = "your class" }, isEditable: Sitecore.Context.PageMode.IsPageEditor))
{
@(item.DestinationUrl != null ? item.DestinationUrl.Text : "Select a link")
}
BeginRenderLink来自Glass:
/// <summary>
/// Render HTML for a link with contents
/// </summary>
/// <typeparam name="T">The model type</typeparam>
/// <param name="model">The model</param>
/// <param name="field">The link field to user</param>
/// <param name="attributes">Any additional link attributes</param>
/// <param name="isEditable">Make the link editable</param>
/// <returns></returns>
public virtual RenderingResult BeginRenderLink<T>(T model, Expression<Func<T, object>> field,
object attributes = null, bool isEditable = false)
{
return GlassHtml.BeginRenderLink(model, field, this.Output, attributes, isEditable);
}