Glass.Mapper not applies additional parameters in

2019-02-28 10:52发布

问题:

For Glass.Mapper BeginRenderLink described as method to render a link that should contain multiple HTML elements: http://glass.lu/docs/tutorial/sitecore/tutorial22/tutorial22.html

What I'd like to add is custom attributes (class, style) to that link:

    <% using (BeginRenderLink(x => x.Image1Link, 
           new NameValueCollection 
           { { "class", "image-banner" }, { "style", string.Format("background-image: url({0})", Model.Image1.Src) } }, true))
       { %>
    <span class="image-banner-wrapper">
        <span class="image-banner-content"><%= Editable(x => x.Image1Text) %></span>
    </span>
    <% } %>

This additional attributes works fine in normal mode but are not displayed in editing mode.

Here is what was found in Glass.Mapper sources for BeginRenderLink:

    if (IsInEditingMode && isEditable)
    {
        return MakeEditable(field, null, model, "haschildren=true", _context, SitecoreContext.Database, writer);
    }
    else
    {
        return BeginRenderLink(field.Compile().Invoke(model) as Fields.Link, attrs, string.Empty, writer);
    }

So if it is editing mode, no additional attributes are applied, only "haschildren=true" is passed.

I wonder is anybody solve that issue somehow?

回答1:

As it seems this is a known issue with Glass and there is already a pull request pending for it (https://github.com/mikeedwards83/Glass.Mapper/pull/73).

I guess the only way to fix it is to get the latest version from GitHub make the fix and recompile the Glass with your fix. Otherwise you can wait for the pull request to get approved and update your Glass version. As you can see the fix is not that hard (taken from here):

if (IsInEditingMode && isEditable)
{   
    if (attrs != null)
    {
        attrs.Add("haschildren", "true");
        return MakeEditable(field, null, model, attrs, _context, SitecoreContext.Database, writer);
    }

    return MakeEditable(field, null, model, "haschildren=true", _context, SitecoreContext.Database, writer);
 }