Function Failed to load resource: Mvc

110 views Asked by At

i try to call a method in my sitefinity project using mvc models, but the program tell Failed to load resource: 404, and never find the controller and i dont know how to resolved, this is my code

Jquery call:

function myFunction(postId) {
    $.ajax({
        url:'/CombosController/GetCantones',
        type: 'POST',
        dataType:"applicaiton/json; charset=utf-8",
        data: { idprovincia: postId },
        success: function(data) {alert("success");},
        error: function() {alert("error");}
    })
}

Action of controller:

[WebMethod]
public static ActionResult GetCantones(int idprovincia)
{
     CombosBL.Provincias.JsonListados metodos = new JsonLIstados();
     List<ClaseCantones.NCanton> ListaPropiedades = metodos.GetCantonesByIdProvincia(idprovincia);
     var lista = ListaPropiedades;
     return new JsonResult { Data = lista, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
1

There are 1 answers

0
Victor Leontyev On

you can call your MVC action in two ways

First way

Example of MVC widget:

[ControllerToolboxItem(Name = "CombosWidget", Title = "CombosWidget", SectionName = "MVC Custom Widgets")]
public class CombosController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult GetCantones(int idprovincia)
    {
        //your logic is here
    }
}

Second way

Creating custom URL route to the action of your controller

Example how to do that for your case. Global.asax.cs code:

    protected void Application_Start(object sender, EventArgs e)
    {            
        Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized);
    }

    protected void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
    {
        if (e.CommandName == "Bootstrapped")
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }

    private void RegisterRoutes(RouteCollection routes)
    {
        routes.Ignore("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "CantonesApi",
            url: "api/getcantones",
            defaults: new { controller = "CombosController", action="GetCantones" }
        );
    }

And after that, you can call http://yourhost/api/getcantones.