PartialView method in BlockController returning only the PartialView instead of the PartialView embedded in the whole page

Vote:
 

I'd appreciate it if anyone had any tips for this.

I have created a simple block for purposes of calculation, using information entered in the block data in the Optimizely CMS edit mode. The idea is that the user enters their information, the calculator form submits on click and the viewmodel is updated with the results of the calculation.

The problem is that while this works:

public override ActionResult Index(ViljahintalaskuriBlock currentBlock)
        {
            var model = new ViljahintalaskuriViewModel()
            {
                ViljahintalaskuriBlock = currentBlock
            };

            // ReSharper disable once Mvc.PartialViewNotResolved
            return PartialView("Viljahintalaskuri", model);
        }

This doesn't:

public ActionResult CalculateData([FromBody] GrainPriceCalculationPostbackDataDTO data)
        {
            // updating model with new values from the form data

            return PartialView("Viljahintalaskuri", model);
        }

The methods are in the same controller, and the latter one returns ONLY the partial view to the page instead of it embedded on the site like when calling the index. I realize this is because of how the CMS calls these controllers to render blocks/pages etc.

What should I do to have my method update the model for that partial view in place and not return ONLY the updated partial view to the browser?

#300380
Apr 19, 2023 13:35
Vote:
 

It's quite difficult to get blocks to post back to themselves as they are partial controllers but there's some old guidance here https://world.optimizely.com/blogs/team-oshyn/dates/2016/3/posting-forms-with-episerver-mvc/ although it's pre .NET core depending on the version you are using. You can get pages to do fairly simple with it such as https://krompaco.nu/2015/01/working-with-your-own-forms-in-episerver-and-mvc/ 

However my suggestion that's the easiest would just be to handle in via a Web API and some JS instead and have your form post to an API without reloading the page. It's a more modern and cleaner way anyhow. Such as https://dev.to/amjadmh73/submit-html-forms-to-json-apis-easily-137l 

#300381
Edited, Apr 19, 2023 16:46
Vote:
 

Your suggestion is probably for the best. I'll make a controller and pass on the block ID to the calls so the right block data is used to make the calculations.

Thanks for your help.

#300422
Apr 20, 2023 4:14
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.