Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Blocks migration from CMS 11 to 12

ZZ
ZZ
Vote:
 

Hi,

We are in process of migrating CMS 11 to 12 on .NET 6. In this process we are having some issues concerning block controller. As in our current solution we are having some block controllers index methods som take paramters.

e.g ->

public class XXtController : BlockControllerBase<XXBlock>
    {


public ActionResult Index(XXBlock xxBlock, string fieldId)
        {
          var viewModel = new xxVIewModel(){
         testfieldId = fieldId
        }
        return View(viewModel);  
}

}

How can this be migrated to AsyncBlockComponent as InvokeComponentAsync doesn't take more than one paramter  ?->

protected override Task<IViewComponentResult> InvokeComponentAsync(XXBlock currentBlock, string fieldId)
        {
            ..............................
        }

Any help would be appreciated

#291575
Edited, Nov 13, 2022 12:01
Vote:
 

Hi, have a look at this blog post: https://andrewlock.net/passing-variables-to-a-view-component/

#291576
Nov 13, 2022 16:47
ZZ
Vote:
 

Thanks for the the link.

In Optimizely InvokeAync() only takes a single parameter of type TContentData, How can I add a second or third paramter to that ? 

 public Task<IViewComponentResult> InvokeAsync(TContentData currentContent)
        {
            return InvokeComponentAsync(currentContent);
        }

        //
        // Summary:
        //     Method that component should implement to render currentContent
        //
        // Parameters:
        //   currentContent:
        //     The current content instance.
        //
        // Returns:
        //     The result from the com
        protected abstract Task<IViewComponentResult> InvokeComponentAsync(TContentData currentContent);
#291577
Nov 13, 2022 18:45
Tomas Hensrud Gulla - Nov 13, 2022 19:11
How are you using that second parameter? What stops you from creating a new InvokeAync() with two parameters?
ZZ
Vote:
 
public class XXComponent: AsyncBlockComponent<XXBlock>
    {
       // This is not possible as InvokeComponentAsync() can only have a single paramter of type TContentData
        protected override async Task<IViewComponentResult> InvokeComponentAsync(XXBlock currentContent, string id)
        {
            var viewModel = new
            {
                id = id,
                currentBlock = currentContent
            };
            return await Task.FromResult(View(viewModel));
        }
     
       // Do you mean like this  ?
        public async Task<IViewComponentResult> InvokeAsync(XXBlock currentContent, string id)
        {
            var viewModel = new
            {
                id = id,
                currentBlock = currentContent
            };
            return await Task.FromResult(View(viewModel));
        }

My component class is inheriting from AsyncBlockComponent and by doing so I also have to implement abstract method InvokeComponentAsync() which takes a single paramter of type TContentData.

I have now created a InvokeAsync() with two parameters but which method would be then get called. I would say its InvokeComponentAsync() as mentioned in Optimizely guidelines.

#291580
Nov 13, 2022 19:43
Tomas Hensrud Gulla - Nov 14, 2022 7:41
What kind of id are you passing to your CMS 11 block controllers? And how?
Andreas J - Nov 14, 2022 22:26
Hmm, I guess you could just create a new view component for this case that inherits from ViewComponent instead. Since you pass the arguments "manually" - maybe there's no need to inherit from AsyncBlockComponent?
ZZ - Nov 16, 2022 9:22
Thanks Andreas, You are right there is no need to inherit from AsyncBlockComponent. If you write your answer as reply to this post then I can accept it as answer
ZZ
Vote:
 

In some of our page views (Razor) we are calling Html extension method 

@Html.XXAction(m => m.Id, Model.CurrentBlock.XXBlock)

This is our extension method which works in CMS 11 ->

 public static HtmlString XXAction<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression,XXBlock xxBlock)
        {
            var Id= helper.ClientIdFor(expression).ToString();
            var htmlBuilder = new StringBuilder();
            htmlBuilder.Append(helper.Action("Index", "XXController",new{xxBlock, Id}));
            return HtmlString.Create(htmlBuilder.ToString());
        }
#291620
Edited, Nov 14, 2022 8:20
Vote:
 

Hmm, I guess you could just create a new view component for this case that inherits from ViewComponent instead. Since you pass the arguments "manually" - maybe there's no need to inherit from AsyncBlockComponent?

#291760
Edited, Nov 16, 2022 9:24
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.