Pregunta

This issue has been discussed many times, but I haven't found a resolution for my particular case.

In one of my Umbraco (6) views I am calling a controller method by using

@Html.Action("Index", "CountryListing");

This results in the "no route in the route table" exception.

I have been fiddling around with the RegisterRoutes method to no avail. I wonder if it is even used as the site still functions when I empty the RegisterRoutes method. This is what it looks like now:

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 }
            );
        }

I have also tried adding an empty "area" to the call like this

@Html.Action("Index", "CountryListing", new {area: String.Empty});

I am using @Html.Action statements in other places and they DO work, so I do understand why some of them work and others don't, but the main problem now is getting my country listing action to work.

¿Fue útil?

Solución

You can solve this by doing the following

  1. Make sure your Controller is extending Umbraco's SurfaceController
  2. Name it YourName*SurfaceController*
  3. Add the [PluginController("CLC")] 'annotation' (I am from Java) to your controller. CLC stands for CountryListController. You can make up your own name of course.
  4. Add the PluginController name (CLC) to the Html.Action call as a "Area" parameter.

My controller:

[PluginController("CLC")]
public class CountryListingSurfaceController : SurfaceController
{
    public ActionResult Index()
    {
       var listing = new CountryListingModel();

       // Do stuff here to fill the CountryListingModel

       return PartialView("CountryListing", listing);
    }
}

My partial view (CountryListing.cshtml):

@inherits UmbracoViewPage<PatentVista.Models.CountryListingModel>

@foreach (var country in Model.Countries)
{
    <span>More razor code here</span>  
}

The Action call:

@Html.Action("Index", "CountryListingSurface", new {Area= "CLC"})

Otros consejos

you can use null instead of using String.empty

@Html.Action("Index", "CountryListing",null);

if you are using area, you have to override RegisterRoutes for every area

public override string AreaName
    {
        get
        {
            return "CountryListing";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "CountryListing_default",
            "CountryListing/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

and i recommend you take a look at the same problem: https://stackoverflow.com/a/11970111/2543986

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top