0

DataController

   public class FilterlistViewComponent : ViewComponent
    {
        
        public async Task<IViewComponentResult> InvokeAsync(string text)
        {
            var result = text;
            return View(result);
        }

    }

Filter.cshtml

<div> 
@await Component.InvokeAsync("Filterlist", new { text = "test" })
</div>

file structure

I applied the following solution exactly, but I am getting the following error.

Equivalent of Html.RenderAction in ASP.NET Core

screenshot of the error

shopxada
  • 3
  • 3
  • Hi @shopxada, where does your FilterlistViewComponent locate? – Rena Apr 08 '22 at 02:29
  • Besides, for your view component `InvokeAsync` method, you return view with `result` and the `result` match the view name not the view model data. So when you pass `new { text = "test" }` to viewcomponent, you will return test view, that is to say you need change `Default.cshtml` to `test.cshtml` or just add a new view `test.cshtml` in your `Data/Components/Filterlist`. – Rena Apr 08 '22 at 02:48

1 Answers1

0

System.InvalidOperationException: A view component named 'Filterlist' could not be found. A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated with 'ViewComponentAttribute' or have a class name ending with the 'ViewComponent' suffix. A view component must not be decorated with 'NonViewComponentAttribute'.

Be sure do not put the ViewComponent class like below which locates inside DataController:

public class DataController : Controller
{
    public IActionResult Filter()
    {
        return View();
    }
    public class FilterlistViewComponent : ViewComponent
    {

        public async Task<IViewComponentResult> InvokeAsync(string text)
        {
            var result = text;
            return View(result);
        }

    }
}

You need put it outside the controller class and it can be located in any other folder.

For example:

enter image description here

Besides, in InvokeAsync method, you return view with result and the result match the view name not the view model data. So when you pass new { text = "test" } to ViewComponent, you will return test view, that is to say you need change Default.cshtml to test.cshtml or just add a new view test.cshtml in your Data/Components/Filterlist.

Rena
  • 30,832
  • 6
  • 37
  • 72