如何解决"公共行动法“methodActionName”控制器“controllerNameController'英寸时未找到; [英] How to solve "public action method 'methodActionName' was not found on controller 'controllerNameController'"
问题描述
我收到此错误
一个公共行动方法consultaCumplimientoOptometras'未找到
控制器NovedadesCumplimientosController。
块引用>下面是整个查看名为 CumplimientoOptometras.cshtml
@ {
ViewBag.Title =Cumplimiento POR Optometras
布局=〜/查看/共享/ _Layout.cshtml
}< H2> Cumplimiento POR Optometras< / H><! - Resultado德拉CONSULTA去Cumplimientos - >
@ Html.Action(consultaCumplimientoOptometras)我在下面的方法在 NovedadesCumplimientosController
[HTTPGET]
公共PartialViewResult consultaCumplimientoOptometras(字符串x)
{
清单< CumplimientoOptometraDTO> lstCumplimientosOpts; lstCumplimientosOpts =新的List< CumplimientoOptometraDTO>(); 返回PartialView(_ consultaCumplimientoOptometras,lstCumplimientosOpts);
}下面是部分视图的 _consultaCumplimientoOptometras.cshtml
@model名单< CumplimientoOptometraDTO><表边框=1>
&所述; TR>
< TD>标识选项和放大器; oacute; METRA< / TD>
< TD>农布雷选项和放大器; oacute; METRA< / TD>
< TD> Horas Laboradas< / TD>
< TD>元Mensual< / TD>
< TD>文塔Mensual< / TD>
< TD>%Cumplimiento< / TD>
< TD>勇气Comisi&放大器; oacute; N< / TD>
< / TR> @foreach(以型号VAR cumpl)
{
&所述; 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>
}
< /表>下面是在 CumplimientoOptometraDTO 类/模型
使用系统;公共类CumplimientoOptometraDTO
{
公众的Int32 idOptometra {搞定;组; }
公共字符串nombreOptometra {搞定;组; }
公众的Int32 horasLaboradas {搞定;组; }
公共小数metaMensual {搞定;组; }
公共小数ventaMensual {搞定;组; }
公共小数porcentajeCumplimiento {搞定;组; }
公共小数valorComision {搞定;组; }
}在文章一开始就提到的错误造成
试图显示时查看 CumplimientoOptometras.cshtmlExactlty在此行
这里是臭名昭著的 RouteConfig
@ Html.Action(consultaCumplimientoOptometras)公共类RouteConfig
{
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
}
}
解决方案在的情况下的 [HTTPGET] 从方法被删除,
也去掉了未使用的参数(字符串x)和它现在的作品// [HTTPGET]
公共PartialViewResult consultaCumplimientoOptometras()
{
清单< CumplimientoOptometraDTO> lstCumplimientosOpts; lstCumplimientosOpts =新的List< CumplimientoOptometraDTO>(); 返回PartialView(_ consultaCumplimientoOptometras,lstCumplimientosOpts);
}我被复制,我看到之前是一样的控制器的方法也被称为/从JQuery的重用吉斯GET请求,事情就是这样的我之所以模式[HTTPGET] 最初添加。
对于这种特殊情况下,因为这code目前不打算从JQuery的叫我可以安全地删除 [HTTPGET] 。尽管如此这感觉更像是并继续工作情景与实际学习或查找根源解决; - )
如果你达到了这个问题寻找这里的人的答案,没有为你工作,随时对这个答案的链接到你的问题发表评论。
在为寻找这个问题,我发现这非常好贴有关如何在相同的控制器方法
接受不同的HTTP动词的答案的http://www.dotnetexpertguide.com/2012/11/aspnet-mvc-acceptverbs-action-filter.html
更新,如果找到真正的原因。
如果你有一个POST(主视图CumplimientoOptometras.cshtml被resturn作为POST操作的结果),开始调用很多层也会发生这种情况,那么调用的 @ Html.Action 甚至的RenderAction 仍然会寻找一个POST方法!这是一个限制/错误,或者我没有理解正确的东西)
所以,如果你想继续接受HTTP GET动词和修复级联POST请求的问题变成一个GET请求添加到您的方法。
的[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
记请即[HTTPGET]相同的的[AcceptVerbs(HttpVerbs.Get)]
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 } ); } }
解决方案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)]
这篇关于如何解决&QUOT;公共行动法“methodActionName”控制器“controllerNameController'英寸时未找到;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!