1

I have a model that contains a parameter called CssClassName like this:

[SitecoreType(TemplateId = "{51D5974A-7ABE-40C3-9EED-E32267C03112}", AutoMap = true)]
    public class Separator
    {
        public virtual Guid ID { get; set; }
        public virtual string Title { get; set; }
        public virtual string CssColorClass { get; set; }
    }

Now, from Sitecore side, this CssColorClass parameter is a DropList that points to a datasource where the template contains two parameters ClassName and Description. How can i tell the CssColorClass to "grab" the ClassName parameter and not the Item name of the items on the datasource?

On my razor view i have this:

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<MyModels.Models.Separator>
@using MyModels.Models
@using System.Web.Optimization
@{ 
    Separator separatorModel = GetRenderingParameters<Separator>();
}

@Styles.Render("~/bundles/separator-css")
<h2 id="@separatorModel.ID" class="separator @separatorModel.CssColorClass">@separatorModel.Title</h2>

2 Answers 2

9

The DropList field only stores the name of the item selected.

To accomplish what you are trying to do, you need to use a Droplink field instead.

The Droplink field will store the ID of the selected item. From there, you can use the ID to find the selected item in Sitecore and convert it to a strongly-typed class that has the ClassName property that you are ultimately trying to retrieve.

Having said all of this above, looks like you're using GlassMapper. If that's the case, it's even easier for you. The type for your property that will map to the Droplink field should be the class that represents the item that will be selected in the Droplink field. GlassMapper will automatically find the item in Sitecore by ID and cast it to the strongly-typed class for you. If no item is selected in the field in Sitecore, GlassMapper will simply set the property to null.

Here's some sample code based on GlassMapper.

Let's say this is the class that represents the type of items that will be selected in the Droplink (I just made up a template ID):

[SitecoreType(TemplateId = "{12121212-1212-1212-1212-121212121212}", AutoMap = true)]
public class CssColor
{
    public virtual Guid ID { get; set; }
    public virtual string Color { get; set; }
}

Here's how you would update your Separator class:

[SitecoreType(TemplateId = "{51D5974A-7ABE-40C3-9EED-E32267C03112}", AutoMap = true)]
public class Separator
{
    public virtual Guid ID { get; set; }
    public virtual string Title { get; set; }
    public virtual CssColor CssColorClass { get; set; }
}
3
  • Could you put a sample using Glass.Mapper (that's what I'm using)
    – Phoenix_uy
    Feb 4, 2017 at 0:42
  • Code samples added, @Phoenix_uy. Let me know if you need any further details. Happy to help.
    – DougCouto
    Feb 4, 2017 at 1:14
  • Thank you @Barbosa! that was the solution for me using Glass.Mapper
    – Phoenix_uy
    Feb 4, 2017 at 4:26
0

I think it would be like this. I hate doing logic in the view, but maybe it will get you to where you need to be. You could also get the Droplink item with Glassmapper.

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<MyModels.Models.Separator>
@using MyModels.Models
@using System.Web.Optimization
@{ 
    Separator separatorModel = GetRenderingParameters<Separator>();
}

 @{
    var styleItem = Sitecore.Context.Database.GetItem(separatorModel.CssColorClass);
    var cssClass = ((Sitecore.Data.Fields.LookupField) styleItem.Fields["ClassName"]).TargetItem["ClassName"];
  }

@Styles.Render("~/bundles/separator-css")
<h2 id="@separatorModel.ID" class="separator @cssClass">@separatorModel.Title</h2>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.