I would like to call specific action method with a parameter and retrieve data from the controller in Umbraco v9.
public class SearchResultController : RenderController
{
private readonly UmbracoHelper UmbracoHelper;
private readonly IPublishedValueFallback PublishedValueFallback;
private ISearchRepository SearchRepository;
public SearchResultController(ILogger<ContentPageController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor,
IPublishedValueFallback publishedValueFallback,
UmbracoHelper umbracoHelper, ISearchRepository searchRepo
)
: base(logger, compositeViewEngine, umbracoContextAccessor)
{
UmbracoHelper = umbracoHelper;
PublishedValueFallback = publishedValueFallback;
SearchRepository = searchRepo;
}
[HttpGet]
public override IActionResult Index()
{
var model = new SearchResult(CurrentPage, PublishedValueFallback);
return View("~/Views/SearchResult.cshtml", model);
}
[HttpPost]
public IActionResult SearchResult(string searchString)
{
var results = SearchRepository.SearchString(searchString);
var model = new SearchResult(CurrentPage, PublishedValueFallback);
return View("~/Views/SearchResult.cshtml", model);
}
}
View:
function Search() {
debugger;
var searchString = document.getElementById("searchLabel").value;
var url = "www.test.com/search?searchString=" + searchString;
location.href = url;
}
I have also tried with @Html.Actionlink method, but I really can't make it work to properly redirect and pass the parameter. If i type the link manually and correctly in browser, the value also gets passed into the controller (debugger shows everything is ok). Thanks for all the Help!
With Umbraco 9 being on dotnet 5+, we now use
ViewComponentsfor this - https://our.umbraco.com/Documentation/Reference/Templating/Mvc/ViewComponentsso for example, you might have a
ViewComponentto render your SearchResult list something like this:Then, create the Default ViewComponent cshtml class in the /Views/Shared/Components/SearchResults directory (named
Default.cshtml) something like this:You can render
ViewComponentsin your templates a couple of different ways, but my favourite is to use the tag-helper format - to do so, register your assembly containing them in your _ViewImports.cshtml file:And then you can you can place them in your templates like so:
Microsoft Docs Reference: View components in ASP.NET Core