Try our conversational search powered by Generative AI!

How to get the category name from block?

Vote:
 

We tagging our blocks in episerver using the built in category.

problem is getting the category name so I can use it in the blocks view.

Tried the code below but it's saying GetCategory isn't available in the controller.

public class ParallaxNavigationBlockController : BlockController<ParallaxNavigationBlock>
{
    public override ActionResult Index(ParallaxNavigationBlock currentBlock)
    {
        var model = new ParallaxNavigationViewModel
        {
            Category = (currentBlock as ICategorizable).Category.GetCategories().FirstOrDefault()
        };

        return PartialView(model);
    }
}

// ViewModel
public class ParallaxNavigationViewModel
{
    public Category Category { get; set; }
}

// Extension method for categories
public static IEnumerable<Category> GetCategories(this CategoryList categoryList)
{
    CategoryRepository categoryRepository = ServiceLocator.Current.GetInstance<CategoryRepository>();
    return categoryList.Select(x => categoryRepository.Get(x));
}
#267016
Nov 19, 2021 4:14
Niamul - Nov 19, 2021 5:19
got it
Vote:
 

First

  1. If you're only expecting 1 category there's no need to load them all to just get the first
  2. Inject the dependency if doing the logic in a controller
  3. The reason why your extension method GetCategories doesn't work is it needs to be in a separate static class

Although you don't need GetCategories I have rewritten including the GetCategories extension if you need it for the future

public class ParallaxNavigationBlockController : BlockController<ParallaxNavigationBlock>
{
    private readonly CategoryRepository _categoryRepository;

    public ParallaxNavigationBlockController(CategoryRepository categoryRepository) // Injected in. The correct way
    {
        _categoryRepository = categoryRepository;
    }

    public override ActionResult Index(ParallaxNavigationBlock currentBlock)
    {
        var firstCategory = (currentBlock as ICategorizable).Category.FirstOrDefault();

        var model = new ParallaxNavigationViewModel
        {
            Category = firstCategory > 0 ? _categoryRepository.Get(firstCategory) : null
        };

        return PartialView(model);
    }
}

// ViewModel
public class ParallaxNavigationViewModel
{
    public Category Category { get; set; }
}

public static class Extensions // Needs to be in a static class
{
    // Extension method for categories
    public static IEnumerable<Category> GetCategories(this CategoryList categoryList)
    {
        CategoryRepository categoryRepository = ServiceLocator.Current.GetInstance<CategoryRepository>();
        return categoryList.Select(x => categoryRepository.Get(x));
    }
}
#267032
Nov 19, 2021 9:25
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.