1
votes

I want to user be able to select background-color using a drop-down list

I've created a new template which inherits from Standard Rendering Parameters as base template and standard value has $name token

Here is template Color definition:
ColorName---DropLink---/sitecore/content/Home/Global/Colors

    [SitecoreType(TemplateId = "{55DB8F6A-807D-48F8-A3D1-D81037938F13}", AutoMap = true)]
public interface IColor:IContentBase
{
    string ColorName { get; set; }
}

I also have created multiple instances of template (Red,Blue,Green)

enter image description here

Then I assign Color template to a ViewRendering as parameter.

enter image description here

Finally, in cshtml file, I try to read this parameter

@using Glass.Mapper.Sc 
@model INews

<H3> Hot News: </H3>
@{
    var rendering = RenderingContext.Current.Rendering;
    string id = rendering.Parameters["ColorName"];
    var context = new SitecoreContext();
   var result=context.GetItem<IColor>(new Guid(id));

}

<div style="background-color: @NewsRepository.GetBackgroundColor(rendering);" class="panel-body">
    <br/>
    <span>@result.ColorName</span><br/>
    @Model.Title
</div>

The result is always {55DB8F6A-807D-48F8-A3D1-D81037938F13} whereas I expect "Green". Also tried to use Sitecore APIs instaed of GlassMapper but still I get Guid .

2

2 Answers

1
votes

Try to use the following

@result.ColorName.Value.Tostring();

3
votes

Since you are using Glass Mapper, you should create models for your rendering parameters in much the same way you would for any other template.

Create a model for the lookup item. I suggest you create a generic type rather than calling it "Color". This will allow you to re-use them for other lookup items from code.

[SitecoreType(TemplateId = "{template-guid}", AutoMap = true)]
public class LookupItemModel : GlassBase
{
    public virtual string Text { get; set; }
}

Create a model for your Rendering Parameters. Make sure you set the TemplateId to your Rendering Parameters template. Note tat the return type of the property is set to LookupItemModel we created above.

[SitecoreType(TemplateId = "{guid-for-rendering-parameter}", AutoMap = true)]
public class ColorParameters
{
    public virtual LookupItemModel Color { get; set; }
}

You can now access the Rendering Parameters as a strongly typed model in your views. Due to the return type being set correctly above, Glass will automatically map the guid to the look up item and you won't have to make a separate context.GetItem() call.

@{
    var parameters = Html.Glass().GetRenderingParameters<ColorParameters>();
    string color = parameters.Color.Text;
}

You can more about using Rendering Parameters in Glass mapper in this article.