C# 在视图中执行处理程序的子请求时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19272720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error executing child request for handler in view
提问by Andre Lombaard
I have an MVC 4 view where I render the following actions
我有一个 MVC 4 视图,我在其中呈现以下操作
@{
Html.RenderAction("Index", "Logo");
Html.RenderAction("Index", "MainMenu");
}
I have a form on my view which is filled out and posted to the controller. In the controller I perform some tasks and then send the model back to my view
我的视图中有一个表格,已填写并发布到控制器。在控制器中,我执行一些任务,然后将模型发送回我的视图
[HttpPost]
public ActionResult Index(ManageAdministratorModel manageAdministratorModel)
{
// I save some of the fields to the database here.
return View(manageAdministratorModel);
}
When I'm redirected to the view I receive the following error
当我被重定向到视图时,我收到以下错误
Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
执行处理程序“System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper”的子请求时出错。
on this line
在这条线上
Html.RenderAction("Index", "Logo");
Any idea why this is happening?
知道为什么会这样吗?
采纳答案by Andre Lombaard
Ok I found the problem, hopefully this will help someone in future.
好的,我找到了问题,希望这会在将来对某人有所帮助。
The controllers for the partial views each contained the [HttpGet]
attribute. For example
每个局部视图的控制器都包含该[HttpGet]
属性。例如
[HttpGet]
public ActionResult Index()
{
}
I remove the attribute from both controllers
我从两个控制器中删除了属性
public ActionResult Index()
{
}
and everything is now working.
现在一切正常。
回答by Darin Dimitrov
Replace:
代替:
return View(manageAdministratorModel);
with:
和:
return PartialView(manageAdministratorModel);
otherwise you might be ending in an infinite loop because you are rendering a view which is attempting to render a view which is attempting to render a view, ...
否则你可能会以无限循环结束,因为你正在渲染一个试图渲染一个试图渲染一个视图的视图,......
Also you might need to remove the [HttpPost]
attribute from your child action.
此外,您可能需要[HttpPost]
从子操作中删除该属性。
回答by Luke
I just got this error occurring in my razor when my partial view had a code formatting error in it.
当我的部分视图中有代码格式错误时,我的剃刀中出现了这个错误。
If you click 'Continue' to get past the error, you'll see the actual error message displayed in the browser window that you loaded it from.
如果您单击“继续”以解决错误,您将在加载它的浏览器窗口中看到实际的错误消息。
Correct the error in the partial view and it'll work!
更正局部视图中的错误,它会起作用!
回答by mtrivedi
The example of "Child Action Only" is:
“仅儿童操作”的示例是:
public class FiltersController : Controller
{
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public ActionResult Departments()
{
string s = "Mahi and kallu";
return View(s);
}
}
**for this am creating 2 views**
1) Index:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Html.Partial("Departments","Filters")
</body>
</html>
**and for Departments View:**
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Departments</title>
</head>
<body>
<div>
@Model
</div>
</body>
</html>
the ***childactions*** can be rendered with the help of "Partial" keyword.
回答by nmit026
I had exactly the same problem, and because I was using attribute routing, the inner exception error message was:
我遇到了完全相同的问题,因为我使用的是属性路由,内部异常错误消息是:
No matching action was found on controller ''.
This can happen when a controller uses RouteAttribute for routing,
but no action on that controller matches the request.
Remove the [HttpGet] attributes from action methods called by Html.Action() and it works. Nothing to do with routing.
从 Html.Action() 调用的操作方法中删除 [HttpGet] 属性,它可以工作。与路由无关。
回答by Jahiron Rodriguez
I had the same error. It began when I changed an action to another controller, so when running the program couldn't find the view in the folder. So if you move an action to another controller, also move the view to the respective folder's controller.
我有同样的错误。当我将一个动作更改为另一个控制器时,它就开始了,因此在运行程序时无法在文件夹中找到视图。因此,如果您将操作移动到另一个控制器,也将视图移动到相应文件夹的控制器。
回答by Lucas
I got this error, but my problem was diferent. To see what is the error about, involve the line you get the error inside a try catch code, like this:
我收到此错误,但我的问题有所不同。要查看错误是什么,请在 try catch 代码中包含您收到错误的行,如下所示:
try
{
@Html.RenderAction("Index", "Logo", new {id = Model.id});
}
catch (Exception e)
{
throw;
}
Execute it with a break point at throw line and check the inner exception of the 'e'. My problem was that I'd changed the parameter name on my Controller and forgot to change it on my View.
在 throw 行使用断点执行它并检查“e”的内部异常。我的问题是我更改了控制器上的参数名称而忘记在视图上更改它。
It's easier to get the error using try catch.
使用 try catch 更容易获取错误。
回答by user2021262
Get rid of the layout @{ Layout = null; }
in the child view.
摆脱@{ Layout = null; }
子视图中的布局 。
回答by Fox Eyes
I was facing the same issue but I put [HTTPGet] attribute over the function name and it worked for me.
我遇到了同样的问题,但我将 [HTTPGet] 属性放在函数名称上,它对我有用。
[HttpGet]
//for Filter parital view
[ChildActionOnly]
public ActionResult Filter()
{
// Your code will come here.
}
回答by Code_Worm
I had this problem, It could happen because render engine can't find any view (corresponding to the name that is given in acton) I'd given wrong name of view (I mistakenly had given action name instead of view name) when I return view name and view model using PartialView()
method, I corrected my view name and it worked fine
我遇到了这个问题,这可能是因为渲染引擎找不到任何视图(对应于在 acton 中给出的名称)我给出了错误的视图名称(我错误地给出了动作名称而不是视图名称)当我使用PartialView()
方法返回视图名称和视图模型 ,我更正了我的视图名称并且它工作正常