如果页面处于编辑模式,其中一个选项是为内容添加空格。它不会使链接为空,但不会显示它的描述。
<% if (IsInEditingMode)
{ %>
<li>
<%=GlassHtml.RenderLink(icon, x => x.Link, isEditable: true, contents: " ") %>
</li>
<% } else {%>
<li>
<%=GlassHtml.RenderLink(icon, x => x.Link, null, true, string.Empty) %>
</li>
<%}%>
另一种选择是编写自己的玻璃扩展。(有关如何执行此类操作的更多信息,您可以查看此线程 - Glass Mapper RenderLink 链接描述 - 如果为空则默认文本)
Glass.Mapper 是开源的,您可以在此处查看渲染链接的实际工作原理:
https://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc/GlassHtml.cs(该方法从第 297 行开始)。
要扩展它,您将需要这样的东西
public virtual string RenderEmptyLinkInEditing<T>(T model, Expression<Func<T, object>> field, object attributes = null, bool isEditable = false, string contents = null)
{
NameValueCollection attrs = null;
if (attributes is NameValueCollection)
{
attrs = attributes as NameValueCollection;
}
else
{
attrs = Utilities.GetPropertiesCollection(attributes, true);
}
var sb = new StringBuilder();
var writer = new StringWriter(sb);
RenderingResult result = null;
if (IsInEditingMode && isEditable)
{
if (contents.IsNotNullOrEmpty())
{
attrs.Add("haschildren", "true");
}
result = MakeEditable(
field,
null,
model,
attrs,
_context, SitecoreContext.Database, writer);
// if (contents.IsNotNullOrEmpty())
// {
// sb.Append(contents);
// }
}
else
{
result = BeginRenderLink(
field.Compile().Invoke(model) as Fields.Link, attrs, contents, writer
);
}
result.Dispose();
writer.Flush();
writer.Close();
return sb.ToString();
}