3

I have created custom variant render called “Success Story”, I create custom variant to display the “description” only, however the view display “ Description field” with no value, when I look at logs I found WARN Could not find variants for component {6D6DD358-64AF-4539-95FA-769A8CDDEB05} from page {4C5BE406-5A93-47BE-83A9-530B19F55C83}

I follow below steps: 1 - Inherit form VariantsController

public class SuccessStoryController : VariantsController
{
    protected ISuccessStoryRepository SuccessStoryRespository
    { get; set; }
    protected ISuccessStoryListRepository SuccessStoryListRespository
    { get; set; }


    protected IVariantsRepository VariantsRespository
    { get; set; }

    public SuccessStoryController()
    {
        SuccessStoryRespository = ServiceLocator.Current.Resolve<ISuccessStoryRepository>();
        SuccessStoryListRespository = ServiceLocator.Current.Resolve<ISuccessStoryListRepository>();
        VariantsRespository = ServiceLocator.Current.Resolve<IVariantsRepository>();
    }

    protected override object GetModel()
    {
        return SuccessStoryListRespository.GetModel();
    }
}


 public class SuccessStoryModel : VariantsRenderingModel
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string Logo { get; set; }
}public class SuccessStoryListModel : VariantListsRenderingModel 
{

    public List<SuccessStoryModel> SuccessStoryItems { get; set; }
}

4 - The view as below

if (!(Model.Rendering.DataSourceItem == null && !Html.Sxa().IsEdit))
{
    <div @Html.Sxa().Component("SuccessStory-list", Model.Attributes)>
        <div class="component-content">

            @Html.Sxa().RenderHeading(Model.HeadingTag, Model.DataSourceItem, "Title", Model.GetRenderingWebEditingParams())

            @if (Html.Sxa().IsEdit && Model.Rendering.DataSourceItem == null)
            {
                @Model.MessageIsEmpty
            }

            @if (Model.Rendering.DataSourceItem != null && !Model.SuccessStoryItems.Any())
            {
                using (Html.Sxa().BeginEditFrame(Html.Sitecore().CurrentRendering.DataSource, "Empty", Model.IsControlEditable))
                {
                    @Model.MessageIsEmpty
                }
            }

            @if (Model.Rendering.DataSourceItem != null)
            {
                <ul>


                    @foreach (var item in Model.SuccessStoryItems)
                    {
                        <div data-itemid="ccc">

                         @foreach (BaseVariantField variantField in Model.VariantFields)
                         {

                            @Html.RenderingVariants().RenderVariant(variantField, Model.Item, true)

                         }


                        </div>
                    }
                </ul>
            }
        </div>
    </div>
}

My parameter template inhert from : enter image description here

My render as below and I add it to available render

enter image description here

my repo

    public class SuccessStoryListRepository : ListRepository, ISuccessStoryListRepository, IModelRepository, IAbstractRepository<IRenderingModelBase>
{
    public override IRenderingModelBase GetModel()
    {
        SuccessStoryListModel model = new SuccessStoryListModel();
        this.FillBaseProperties(model);
        //BindSuccessStory();
        model.SuccessStoryItems = BindSuccessStory(); //this.GetItems().Select<Item, SuccessStoryModel>((Func<Item, SuccessStoryModel>)(item => new SuccessStoryModel()));
        return (IRenderingModelBase)model;
    }

    private List<SuccessStoryModel> BindSuccessStory()
    {
       var  items=this.GetItems().ToList();

        List<SuccessStoryModel> list = new List<SuccessStoryModel>();


        foreach (var item in items)
        {
            //item.Fields["Description"].Value;

            list.Add(new SuccessStoryModel
            {
                Description = item.Fields["Description"].Value,
                Title= item.Fields["Title"].Value

            });
        }
        return list;

    }

}

2 Answers 2

0

Ok - it looks like you are rendering this out wrong. Take a look at your loop:

@foreach (var item in Model.SuccessStoryItems)
{
    <div data-itemid="ccc">
     @foreach (BaseVariantField variantField in Model.VariantFields)
     {
        @Html.RenderingVariants().RenderVariant(variantField, Model.Item, true)
     }
    </div>
}

So you are looping through Model.SuccessStoryItems - but then in your look, you are passing in Model.Item - that will be the same item for every entry in the loop. It looks like you should be passing in the item variable you are declaring in your loop.

But when you have created your SuccessStoryModel, you have added fields for Description and Title. The @Html.RenderingVariants().RenderVariant() method, needs a Sitecore Item, so you will need to modify the code to pass this in.

First, change your SuccessStoryListModel like to have a list of Item objects like this:

public class SuccessStoryListModel : VariantListsRenderingModel 
{
    public List<Item> SuccessStoryItems { get; set; }
}

Now modify your BindSuccessStory method to populate that list like this:

private List<Item> BindSuccessStory()
{
    var  items = this.GetItems().ToList();

    var list = new List<Item>();
    list.AddRange(items);
    return list;
}

Finally, modify your view to use that item like this:

@foreach (var successStoryItem in Model.SuccessStoryItems)
{
    <div data-itemid="ccc">
     @foreach (BaseVariantField variantField in Model.VariantFields)
     {
        @Html.RenderingVariants().RenderVariant(variantField, successStoryItem, true)
     }
    </div>
}

Now you are passing in the item that has the field you want to be rendered correctly and it should get rendered as the variant field.

0
0

Messages like "FIELD_NAME field" (e.g. "Description field" like in your case) means that the field specified in the variant item doesn't exist on the context item passed to the variant renderer. As far as I can see you are rendering variants like that:

@Html.RenderingVariants().RenderVariant(variantField, Model.Item, true)

so ensure that Model.Item is exactly the item you want to use and if it contains "Description" field (or any other field which you'll specify in the variant items).

2
  • my looks like below , do i miss anything ? Oct 2, 2017 at 9:36
  • thanks for your help, i add my repo class can you help me on what is wrong there ? Oct 2, 2017 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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