MVC6 ViewComponents Simple example of results in "Could not find method 'Invoke' matching parameters".

I'm trying to find a new ViewComponents function in the simplest sense of the word and I'm having problems and lack of documentation doesn't help.

My first goal was to call my ViewComponent with a list of strings and display them.

Literally the only guide I can find about ViewComponents is here and what I have based everything on so far: http://www.asp.net/vnext/overview/aspnet-vnext/vc#intro

I just want to display a list of strings that I pass to it in the invoke method, but cannot seem to get the invoke method to work in the first place.

My folder / file structure:

enter image description here

My very simple code looks like this:

VcExampleViewComponent.cs

using Microsoft.AspNet.Mvc;

namespace Mvc6Example3.ViewComponents
{
    public class VcExampleViewComponent : ViewComponent
    {
        public VcExampleViewComponent()
        { 
        }

        public IViewComponentResult Invoke(string[] strArr)
        {
            return View(strArr);
        }
    }
}

      

Default.cshtml (the ViewComponent is supposed to be rendered)

@Model String[]

<h3>Strings passed in:</h3>
@foreach (String str in Model)
{
    <span>@str</span>
}

      

Index.cshtml (where the ViewComponent is called)

@{
    ViewBag.Title = "Home Page";
}

<div class="jumbotron">
    <h1>ViewComponent Example</h1>
</div>

@Component.Invoke("VcExample", new String[]{"hello", "hola", "bonjour" })

      

The error I am getting:

500 Internal server error

System.InvalidOperationException

Could not find method "Invoke" matching the parameters.

at Microsoft.AspNet.Mvc.ViewComponents.DefaultViewComponentInvoker.Invoke (ViewComponentContext context) at Microsoft.AspNet.Mvc.ViewComponents.DefaultViewComponentHelper.InvokeCore (Writer TextWriter, handle to ViewComponentDescriptor, arguments ObjectA is []) in .Invoke (linename, Object [] arguments) in Asp.ASPV__Views_Home_Index_cshtml.d__13.MoveNext () in /Views/Home/Index.cshtml:line 9

It might be something really trivial that I just forgot but can't find it. Thanks for the help!

+3


source to share





All Articles