0

Hi I'm currently using Sitecore 8.1 update 1 with MVC5.2.3 and Glass Mapper. I'm having some issues with the Glass Mapper link field. I have created a template which is derived from Standard Rendering Parameters template in which I have used Sitecore General Link field. Also I have created Model for that:

My model:

[SitecoreType(TemplateId = "{912B074D-F8BA-4AA7-9276-016515A1ACE8}")]
public class RelatedArticleParams
{
     [SitecoreId]
     public virtual Guid Id { get; set; }

     public virtual string HeaderText { get; set; }

     [SitecoreField(FieldType = SitecoreFieldType.GeneralLink)]
     public Link Link { get; set; }
}

My View:

@{
    var parameters = GetRenderingParameters<RelatedArticleParams>();
}
<a href="@parameters.Link.Url" class="linkdark">@parameters.Text</a>

Everything is fine if I add the link from presentation details at Sitecore backend. But when I click on this component at Sitecore Page Experience Editor and insert link to Rendering Parameters, then it will give An error occurred red line indication at top of the page. I cant insert link from Page Editor mode.

Please help me in this issue whether it is Glass Mapper bug or I am making any mistake ??

Thanks. Will appreciate your suggestions.

Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
  • Are there any errors in your log files? Check the browser console and the AJAX/xhr calls that are made and check the messages of the response to tell us what error is being thrown. – jammykam Feb 22 '16 at 11:10
  • Your `RelatedArticleParams` class has a `HeaderText` property, but your view uses `@parameters.Text`. Was this just a typo when creating the SO question or is that the real code? – Ben Golden Feb 22 '16 at 15:00
  • Use the BeginRenderLink or Editable. – ASura Aug 30 '16 at 16:30

3 Answers3

4

Another option is to do it in code.

public class MyViewModel
{
    public HtmlString MyLink { get; set; }
}
public class MyController : Controller
{
    private readonly IGlassHtml _glassHtmlHelper;
    public void MyController()
    {
        _glassHtmlHelper = new GlassHtml(new SitecoreContext());
    }
    public ViewResult MyControllerAction()
    {
        var viewModel = new MyViewModel();
        //Get your item
        viewModel.MyLink = new HtmlString(_glassHtmlHelper.RenderLink<RelatedArticleParams>(contentItem, x => x.Link, isEditable: true));
        return View(viewModel);
    }
}

Then, in your markup, all you have to do is:

@model MyViewModel
<div>
    @Model.MyLink
</div>
LonghornTaco
  • 452
  • 2
  • 14
2

You should use
@Editable(Property name of glass mapper) //using the Model property.

Reference

Pankaj Tiwari
  • 1,378
  • 11
  • 13
2

use

@RenderLink(x => x.Link)

or

@using (BeginRenderLink(x => x.GeneralLink, isEditable: true))
{
    @RenderImage(x => x.Image)
}

http://glass.lu/Mapper/Sc/Tutorials/Tutorial22

luckymow8
  • 72
  • 3