Good afternoon, I'm using ASP.NET MVC 5 with Windsor and when an Ajax request calls a method in the controller, the error saying that the controller has unmet dependencies. How do I make the request work because the dependencies are resolved in the constructor?
In the Controller I declare the variables:
private readonly IMaterial _material;
private readonly IFamilia _familia;
private readonly IUnidade _unidade;
private readonly IAlterarMaterial _alterarMaterial;
Then I have the Controller driver:
public MaterialController(IMaterial material, IFamilia familia, IUnidade unidade, IAlterarMaterial alterarMaterial)
{
_material = material;
_unidade = unidade;
_familia = familia;
_alterarMaterial = alterarMaterial;
}
And then I have the method that will be called by Ajax.
public ActionResult ListarMaterial(String nome)
{
List<Material> lista = _material.Listar(nome).ToList();
return View(lista);
}
In Global.asax I have these two methods
private static void RegisterWindsor()
{
container = new WindsorContainer().Install(FromAssembly.This());
var controllerFactory = new WindsorControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
}
protected void Application_End()
{
container.Dispose();
}
And no applications_Start
RegisterWindsor();
ControllerFactory class
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IKernel kernel;
public WindsorControllerFactory(IKernel kernel)
{
this.kernel = kernel;
}
public override void ReleaseController(IController controller)
{
kernel.ReleaseComponent(controller);
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
}
return (IController)kernel.Resolve(controllerType);
}
}
Called Ajax
function Pesquisar() {
$('.corpoTbl').remove();
$.ajax({
url: "/Material/ListarMaterial",
type: "POST",
data: { nome: $('#pesquisa').val() },
success: function (data) {
if (!data.ok){
window.alert("erro");
}
else {
$('#tabela').html(data);
}
}
});