I have an error that has been running me up the walls the past couple of days. I have some code that takes an HttpContext and a ViewResult and renders the view into the string. This code seems to work very well, until I introduce a view that includes an @Html.RenderAction call. When the RenderAction call is added to the view, I get an error message stating that the controller tied to the action needs a parameter-less constructor. Adding such a constructor, however, does not improve the situation. The code that performs the render follows:
public static string Render(HttpContextBase httpContext, ViewResultBase partialViewResult)
{
var routeData = new RouteData();
routeData.Values.Add("controller", "mock");
var context = new ControllerContext(httpContext, routeData, new EmptyController());
var view = partialViewResult.View;
if (view == null)
{
result = partialViewResult.ViewEngineCollection.FindPartialView(context, partialViewResult.ViewName);
view = result.View;
}
using (var sw = new StringWriter())
{
var viewContext = new ViewContext(context, view, partialViewResult.ViewData,
partialViewResult.TempData, sw);
view.Render(viewContext, sw);
if (result != null)
result.ViewEngine.ReleaseView(context, view);
return sw.ToString();
}
}
In the view I am trying to render, I have tried:
@{ Html.RenderAction(FormController.Directive(Model.FormModel)); }
and
@Html.Action(FormController.Directive(Model.FormModel))
Both result in the same error messaging.
An error occurred when trying to create a controller of type 'FormController'. Make sure that the controller has a parameterless public constructor.
Removing the action call altogether gets rid of the error message.
The action in the controller looks like:
public virtual ActionResult Directive(FormModel model)
{
model.MyProperty = "test";
return View("FormView", model);
}
Any advice would be greatly appreciated.
UPDATE
I traced this problem to having some sort of tie to Unity. The render code works when I add an empty constructor and remove all of the other constructors. The other constructors included references to interfaces that are routed through Unity. I will need to learn more about Unity. There may be some functionality there that is replacing or destroying my controller's default constructor.
Html.Action
声明应类似于以下内容:
@Html.Action("Directive", "Form", new { model = Model.FormModel })
/ add default constructor into your "FormModel" class, it will solve the problem /
public void FormModel()
{
}
As commented on earlier, the error is related to our use of Unity IOC. It looks like this is a known problem with MVC when using dependency injected controllers. There is a proposed fix on CodeProject: http://www.codeproject.com/Articles/99361/How-To-Use-Unity-Container-In-ASP-NET-MVC-Framewor
I will try this and hope that it corrects the problem for us.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.