Hi friends,
I implemented a component with a list with products in my website built on Umbraco 12. My issue comes when i try to sort my products by price because i need a custom controller, right? So i tried to create a custom controller which inherits Surface Controller, also i created a view model like: public List productlist {get; set;}. In my controller i tried to access my product list from CurrentPage?.Value<IEnumerable>("productList")?.ToList(); and return Ok(vm). When i inherit my viewModel to my view, website is crashed "ModelBindingException: Cannot bind source type Umbraco.Cms.Web.Common.PublishedModels.HomePage to model type Ikea.ViewModel.Products.". Do you have any idea how to solve this issue or do you have an exemple file which implements a custom controller? More than that, how can i call my methods from controller in view? with or what version? Thanks.
s
SiempreSteve
12/01/2023, 10:39 AM
Sounds like you want to use a view component.
https://docs.umbraco.com/umbraco-cms/v/11.latest/reference/templating/mvc/viewcomponents
THe way I have this working is the block in the list has a partial - this calls my view componment which then gives you access to all the Umbraco helpers and ability to create a view model etc - that then renders the view compnent view.
A bit confusing as a partial triggers a view component (which I think of as a mini controller) that then renders another "partial". But it works.
So your block partial
@(await Component.InvokeAsync("FullWidthFundBlock", new { block = block, currentPage = currentPage }))
Where FullWidthFundBlock has a correspond FullWidthFundBlockViewComponent.cs
public IViewComponentResult Invoke(FullWidthFundBlock block, IPublishedContent currentPage)
{
SiempreSteve
12/01/2023, 10:39 AM
...
return View("FullWidthFundBlock", viewModel);
SiempreSteve
12/01/2023, 10:40 AM
Then you have your end partial /views/Components/FullWidthFundBlock/FullWidthFundBlock.cshtml
This inherits:
@model FullWidthFundBlockViewModel
and does the usual rendering.
HTH