2

I am getting this error

A public action method 'consultaCumplimientoOptometras' was not found on controller 'NovedadesCumplimientosController'.

Here is the whole View called CumplimientoOptometras.cshtml

@{
    ViewBag.Title = "Cumplimiento por Optometras";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Cumplimiento por Optometras</h2>

<!-- Resultado de la Consulta de Cumplimientos -->
@Html.Action("consultaCumplimientoOptometras") 

I have the following method in the NovedadesCumplimientosController

[HttpGet]
public PartialViewResult consultaCumplimientoOptometras(String x)
{
    List<CumplimientoOptometraDTO> lstCumplimientosOpts;

    lstCumplimientosOpts = new List<CumplimientoOptometraDTO>();

    return PartialView("_consultaCumplimientoOptometras", lstCumplimientosOpts);
}

Here is the partial view _consultaCumplimientoOptometras.cshtml

@model List<CumplimientoOptometraDTO>

<table border="1">
    <tr>
        <td>Id Opt&oacute;metra</td>
        <td>Nombre Opt&oacute;metra</td>
        <td>Horas Laboradas</td>
        <td>Meta Mensual</td>
        <td>Venta Mensual</td>
        <td>% Cumplimiento</td>
        <td>Valor Comisi&oacute;n</td>
    </tr>

    @foreach (var cumpl in Model)
    {
        <tr>
            <td>@cumpl.idOptometra</td>
            <td>@cumpl.nombreOptometra</td>
            <td>@cumpl.horasLaboradas</td>
            <td>@cumpl.metaMensual</td>
            <td>@cumpl.ventaMensual</td>
            <td>@cumpl.porcentajeCumplimiento</td>
            <td>@cumpl.valorComision</td>
        </tr>
    }
</table>

Here is the CumplimientoOptometraDTO class/model

using System;

public class CumplimientoOptometraDTO
{
    public Int32 idOptometra { get; set; }
    public String nombreOptometra { get; set; }
    public Int32 horasLaboradas { get; set; }
    public Decimal metaMensual { get; set; }
    public Decimal ventaMensual { get; set; }
    public Decimal porcentajeCumplimiento { get; set; }
    public Decimal valorComision { get; set; }
}

The error mention at the very beginning of the post is caused when trying to display the View CumplimientoOptometras.cshtml

Exactlty on this line

And here is the infamous RouteConfig

@Html.Action("consultaCumplimientoOptometras") 

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99

2 Answers2

1

In the case the [HttpGet] was removed from the method and also removed the unused parameter (String x) and it now works

    //[HttpGet]
    public PartialViewResult consultaCumplimientoOptometras()
    {
        List<CumplimientoOptometraDTO> lstCumplimientosOpts;

        lstCumplimientosOpts = new List<CumplimientoOptometraDTO>();

        return PartialView("_consultaCumplimientoOptometras", lstCumplimientosOpts);
    }

I was replicating I pattern that I saw before were the same Controller method was also being called/reused from JQuery gith a GET request and that was the reason that [HttpGet] was initially added.

For this particular case since this code is currently not going to be called from JQuery I can safely remove the [HttpGet]. Still this feels more like a "solved with actually learning or finding root cause" and keep working scenario ;-)

If you reach this question looking for an answer and none of the ones here work for you feel free to comment on this answer with a link to your problem

While looking for an answer for this problem I found this very nice post about how to accept different HTTP VERBS on the same controller method

http://www.dotnetexpertguide.com/2012/11/aspnet-mvc-acceptverbs-action-filter.html

UPDATE if found the REAL REASON

This can also happen if you have many layers of calls that start with a POST (the main view CumplimientoOptometras.cshtml was resturn as a result of a POST action), then the call to @Html.Action or even RenderAction will still look for a POST method !! is this a limitation/bug or something that I am not understanding correctly)

So if you want to continue to accept the HTTP GET verb and fix the problem of cascading post request into a get request add this to your method

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]

Keep in mind that [HttpGet] is the same as [AcceptVerbs(HttpVerbs.Get)]

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • 1
    There should be no magic here. If your browser sent a POST request over the wire, and you have an `HttpGet` decorated method, you'll get this error. [Html.Action](https://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.action(v=vs.118).aspx#M:System.Web.Mvc.Html.ChildActionExtensions.Action(System.Web.Mvc.HtmlHelper,System.String)) clearly does NOT result in a separate request, so if you are in a POST request right now, the error you are getting is quite expected. You might consider using `ChildActionOnly` attribute instead. – Andrew Savinykh Jan 07 '16 at 03:29
  • zespri - The main view was return as part of a POST request but that view containes a GET request to obtain the results of different request. maybe my answer needs a better wording/writting – Mauricio Gracia Gutierrez Jan 07 '16 at 03:31
  • What I mean, is that if you look at your Fiddler trace, you will (most likely) discover that calling this Action is a part of a POST request, not a GET one. Try it! – Andrew Savinykh Jan 07 '16 at 03:33
  • @zespri - please post your answer here - http://stackoverflow.com/questions/16941317/a-public-action-method-was-not-found-on-controller/34646799#34646799 since is more active than my own question – Mauricio Gracia Gutierrez Jan 07 '16 at 03:37
  • In my case `[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]` works but `HttpGet, HttpPost]` does not – Andrii Sep 16 '16 at 07:40
  • @Andrii in order to accept more than one verb you need to use the "AcceptVerbs" sintax – Mauricio Gracia Gutierrez Sep 16 '16 at 14:48
0

This usually happens when you define your child action method inside one controller( Ex : HomeController) and try to call this method from a view belongs to another controller ( Ex : NovedadesCumplimientosController).

You can use another overload of Html.Action() method where you will specify the both the action name and controller name.

@Html.Action("consultaCumplimientoOptometras","Home") 

Assuming your consultaCumplimientoOptometras is defined inside HomeController

Shyju
  • 214,206
  • 104
  • 411
  • 497