This is a migrated thread and some comments may be shown as answers.

[Solved] Actions require parameterless controller?

4 Answers 202 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tom
Top achievements
Rank 1
Tom asked on 30 Jan 2012, 06:59 AM
When using MVC's IoC/Depency Injection support it's easy (and often desirable) to create Controllers that use constructor injection to receive needed services.

It appears that at least the Grid control requires Action targets to be in a controllers that has a parameterless constructor.  It's easy enough to create a parameterless constructor but seems a little odd that I should have to do that.

Am I missing something?  I'm not sure why constructors are bothered with when simply creating a link.. but if it is indeed needed is there some what I can provide my IoC object (or hook in to MVC's System.Web.Mvc.DependencyResolver)?

-Tom

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 30 Jan 2012, 09:35 AM
Hi,

 Why do you think that the Grid control requires action methods to be in controllers with parameterless  
constructor? Could you please clarify what the problem is?

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Tom
Top achievements
Rank 1
answered on 30 Jan 2012, 10:42 AM
Here's a really simple example:

If I create a simple controller like so:

    public class PlaygroundController : Controller
    {
        public PlaygroundController(ICompanyService injected)
        {
        }
 
        public ActionResult Index()
        {
            return View();
        }
}

and create a simple menu nav in a view (or _Layout.cshtml) that references the Index Action of the playground controller like so:
@{ Html.Telerik().Menu()
     .Name("Menu")
     .Items(menu =>
       {
         menu.Add().Text("test").Action("index", "Playground", new { area = "" });
       })
     .Render();                  
}

I get System.MissingMethodException with "No parameterless constructor defined for this object." message when it renders the view.
Here is the stacktrace:
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at Telerik.Web.Mvc.Infrastructure.Implementation.ControllerContextCache.ControllerContextFactory(RequestContext requestContext, String controllerName, String areaName)
at Telerik.Web.Mvc.Infrastructure.Implementation.ControllerContextCache.<>c__DisplayClass1.<GetControllerContext>b__0()
at Telerik.Web.Mvc.Infrastructure.Implementation.NoCache.Get[T](String key, Func`1 defaultValueFactory)
at Telerik.Web.Mvc.Infrastructure.Implementation.ControllerContextCache.GetControllerContext(RequestContext requestContext, String controllerName, String areaName)
at Telerik.Web.Mvc.Infrastructure.Implementation.AuthorizationContextCache.AuthorizationContextFactory(RequestContext requestContext, String controllerName, String actionName, String areaName)
at Telerik.Web.Mvc.Infrastructure.Implementation.AuthorizationContextCache.<>c__DisplayClass1.<GetAuthorizationContext>b__0()
at Telerik.Web.Mvc.Infrastructure.Implementation.NoCache.Get[T](String key, Func`1 defaultValueFactory)
at Telerik.Web.Mvc.Infrastructure.Implementation.AuthorizationContextCache.GetAuthorizationContext(RequestContext requestContext, String controllerName, String actionName, RouteValueDictionary routeValues)
at Telerik.Web.Mvc.Infrastructure.Implementation.ControllerAuthorization.IsAccessibleToUser(RequestContext requestContext, String controllerName, String actionName, RouteValueDictionary routeValues)
at Telerik.Web.Mvc.Infrastructure.Implementation.NavigationItemAuthorization.IsAccessibleToUser(RequestContext requestContext, INavigatable navigationItem)
at Telerik.Web.Mvc.UI.NavigatableExtensions.IsAccessible(INavigatable item, INavigationItemAuthorization authorization, ViewContext viewContext)
at Telerik.Web.Mvc.UI.NavigationItemContainerExtensions.WriteItem[TComponent,TItem](TItem item, TComponent component, IHtmlNode parentTag, INavigationComponentHtmlBuilder`1 builder)
at Telerik.Web.Mvc.UI.Menu.<>c__DisplayClass4.<WriteHtml>b__3(MenuItem item)
at Telerik.Web.Mvc.Extensions.EnumerableExtensions.Each[T](IEnumerable`1 instance, Action`1 action)
at Telerik.Web.Mvc.UI.Menu.WriteHtml(HtmlTextWriter writer)
at Telerik.Web.Mvc.UI.ViewComponentBase.Render()
at Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.Render()
at ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\...\_Layout.cshtml:line 37
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.WebPages.WebPageBase.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer)
at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
at System.Web.WebPages.WebPageBase.Write(HelperResult result)
at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
at System.Web.WebPages.WebPageBase.PopContext()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
0
Atanas Korchev
Telerik team
answered on 30 Jan 2012, 10:54 AM
Hello,

 This is probably caused by a well known issue. Upgrading to the latest internal build should resolve this problem.

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Tom
Top achievements
Rank 1
answered on 30 Jan 2012, 11:04 AM
Thanks.  I'll give it a go.  I *am* using 2011.3.1306.340 so it's likely you're right.
Tags
General Discussions
Asked by
Tom
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Tom
Top achievements
Rank 1
Share this question
or