Glass.Mapper not applies additional parameters in BeginRenderLink method

佐手、 提交于 2019-12-02 07:11:09

问题


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);
 }


来源:https://stackoverflow.com/questions/25283843/glass-mapper-not-applies-additional-parameters-in-beginrenderlink-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!