Reputation: 10844
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ómetra</td>
<td>Nombre Optómetra</td>
<td>Horas Laboradas</td>
<td>Meta Mensual</td>
<td>Venta Mensual</td>
<td>% Cumplimiento</td>
<td>Valor Comisió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 }
);
}
}
Upvotes: 2
Views: 10361
Reputation: 10844
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)]
Upvotes: 1
Reputation: 218722
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
Upvotes: 0