I've created an extension method to work around it.
Note that I've extended GlassHtml
and not GlassView
because you may want to pass a different model type than the one that's used for the view.
namespace ParTech.MvcDemo.Context.Extensions
{
using System;
using System.Linq.Expressions;
using System.Web;
using Glass.Mapper.Sc;
using Glass.Mapper.Sc.Fields;
public static class GlassHtmlExtensions
{
public static HtmlString RenderLinkWithDefaultText<T>(this GlassHtml glassHtml, T model, Expression<Func<T, object>> field, object attributes = null, bool isEditable = true, string defaultText = null)
{
var linkField = field.Compile().Invoke(model) as Link;
if (linkField == null || string.IsNullOrEmpty(linkField.Text))
{
return new HtmlString(glassHtml.RenderLink(model, field, attributes, isEditable, defaultText));
}
return new HtmlString(glassHtml.RenderLink(model, field, attributes, isEditable));
}
}
}
You can now do this in your view:
@(((GlassHtml)this.GlassHtml).RenderLinkWithDefaultText(MyModel, x => x.LinkField, null, true, "Static default text"))
Still a bit hacky because you need to cast the IGlassHtml
to GlassHtml
, but it works.
If you always have the correct model defined for you view (and thus don't need to specify the model parameter) you could put this extension method on GlassView
.