How can I return block as partial from action in ControllerBlock?

Vote:
 
public class ProductSearchBlockController : BlockController<ProductSearchBlock>
    {
        private readonly IRepository<Products> _productsRepository;

        public ProductSearchBlockController(IRepository<Products> productsRepository)
        {
            _productsRepository = productsRepository;
        }

        public override ActionResult Index(ProductSearchBlock currentBlock)
        {
            var viewModel = new ProductSearchBlockViewModel();
            viewModel.Products = _productsRepository.All().ToList();

            return PartialView(viewModel);
        }

        [HttpGet]
        public ActionResult OrderBy()
        {
            var viewModel = new ProductSearchBlockViewModel();
            viewModel.Products = _productsRepository.All().OrderBy(p => p.Price).ToList();

            return PartialView("../ProductSearchBlock/Index",viewModel);
     

This is my controller I have a form in the block that hits the OrderBy action however what is returned is the partial file not the page with the block. How can achieve this?

I want to pass in sorted products in the block. When a button is clicked.

#264413
Oct 04, 2021 14:37
Vote:
 

This post might be helpful for you: How to post to block controller action? | Optimizely Developer Co

if not, please post the code from your form

#264415
Edited, Oct 04, 2021 15:42
Vote:
 

Hi,

Just change the [HttpGet] to [HttpPost] and function name to Index.

public class ProductSearchBlockController : BlockController<ProductSearchBlock>
    {
        private readonly IRepository<Products> _productsRepository;

        public ProductSearchBlockController(IRepository<Products> productsRepository)
        {
            _productsRepository = productsRepository;
        }

        public override ActionResult Index(ProductSearchBlock currentBlock)
        {
            var viewModel = new ProductSearchBlockViewModel();
            viewModel.Products = _productsRepository.All().ToList();

            return PartialView(viewModel);
        }

      [HttpPost]
      public override ActionResult Index(ProductSearchBlock currentBlock)
        {
            var viewModel = new ProductSearchBlockViewModel();
            viewModel.Products = _productsRepository.All().OrderBy(p => p.Price).ToList();

           return PartialView(viewModel);
     }
}
#264417
Oct 04, 2021 16:36
* 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.