Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon 3 posts 53 karma points
    Jul 13, 2013 @ 19:32
    Simon
    0

    RenderMvcController and partial views

    I need to merge some none Umbraco news content into my Umbraco site.  I want to have news page that lists the stories and a sidebar widget that I can add to other pages, which I intended to build as a partial view.

    I created the following controller....

    public class NewsControllerUmbraco.Web.Mvc.RenderMvcController

    {

    public ActionResult News(RenderModel renderModel)
    {

            ...code goes here...

    return View(model);
    }

    [ChildActionOnly]
    public ActionResult Widget()

    {
            ...code goes here...

    return PartialView(model);
    }

    }

    The news page works fine, but I'm having trouble with the partial view.

    In my cshtml files I'm using...

    @Html.Action("Widget", "News")

    ...but I always get "No route in the route table matches the supplied values."

    I've been trying to get this to work for most of my Saturday afternoon, so any help is very much appreciated!

    I'm quite new to MVC, so apologies if I've made a really stupid mistake :)

  • Simon 3 posts 53 karma points
    Jul 13, 2013 @ 21:33
    Simon
    100

    Got it working now!

    In case anyone with the same problem finds this through Google, I just needed to register the route.

    Create a class that inherits IApplicationEventHandler with the following...


            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)

            {

                RouteTable.Routes.MapRoute(

                    "News",

                    "News/Widget",

                    new { controller = "News", action = "Widget" }

     

                );

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 14, 2013 @ 20:58
    Jeroen Breuer
    0

    You can also do it without adding an extra route. If you post it to a SurfaceController it will also work. You can also use a SurfaceController for route hijacking so you can have your "public ActionResult News(RenderModel renderModel)" method and "[ChildActionOnly] public ActionResult Widget()" method in the same controller: http://issues.umbraco.org/issue/U4-2342

    Jeroen

  • Rob Watkins 369 posts 701 karma points
    Jun 10, 2014 @ 15:00
    Rob Watkins
    0

    Old question I know, but this has just helped me out moving from webforms.

    Is this simply a limitation of ASP.NET MVC? Because coming from a usercontrols background it seems a bit insane to have to programmatically assign routes on application startup just to include a reusable code snippet.

    I know I can create a SurfaceController instead, but one of the great advantages of Umbraco was always that you didn't have to create CMS specific wrappers for every tiny piece of functionality.

    I freely admit I am finding MVC in general a total nightmare to get my head round (and the help available on the web to be woeful), so I'm probably just missing some key concepts.

Please Sign in or register to post replies

Write your reply to:

Draft