Copied to clipboard

Flag this post as spam?

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


  • Graham Davis 110 posts 376 karma points
    Aug 14, 2017 @ 06:09
    Graham Davis
    0

    How to pass data to view from an external source

    Our company supports the Real Estate industry and I an trying to create a page in Umbraco that load the details of the listing from an id passed in the url. I have tried several examples so far and am lost as to what the missing piece is. The data is retrieved from an external MLS database via a REST API.

    I have a custom route that looks like this:

    RouteTable.Routes.MapRoute(
               name: "PropertyDetails",
               url: "real-estate/{description}/{address}/{propertyID}",
               defaults: new { controller = "Property", action = "Index", propertyID = UrlParameter.Optional }
           );
    

    For my Controller, I have tried RenderMvcController, SurfaceController, and PluginController. It currently looks like this:

    public class PropertyController : Umbraco.Web.Mvc.PluginController
    
    {
        public PropertyController()
            : this(UmbracoContext.Current)
        {
        }
    
        public PropertyController(UmbracoContext umbracoContext)
            : base(umbracoContext)
        {
        }
    
        public ActionResult Index(string propertyID)
        {
            dbzBest.Models.PropertyDetails p = new dbzBest.Models.PropertyDetails();
    
            umbraco.NodeFactory.Node node = umbraco.uQuery.GetNodesByName("real-estate").FirstOrDefault();
            IPublishedContent currentNode = Umbraco.Content(node.Id);
    
            p = zbdLibrary.GlobalFunctions.zbdAPI("property/GetDetails/" + propertyID).jsonToEntity<dbzBest.Models.PropertyDetails>();
    
    
            //***Using this line returns page as expected, but no info on the property.  I get null errors with out it.
            //return View("real-estate", CreateRenderModel(currentNode));
    
            //this line fails.  How do I pass this data to the view?
            return View("real-estate", p);
        }
    
        private RenderModel CreateRenderModel(IPublishedContent content)
        {
            var model = new RenderModel(content, CultureInfo.CurrentUICulture);
            //add an umbraco data token so the umbraco view engine executes
            RouteData.DataTokens["umbraco"] = model;
            return model;
        }
    }
    

    Can someone please tell me what I am missing?

    Thanks

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Jun 18, 2022 @ 20:08
    Marc Goodson
    100

    Hi Graham

    The problem here is your Umbraco specific controllers need the context of an IPublishedContent item to accompany the routing...

    There is a special way to map such a route through umbraco using "MapUmbracoRoute' instead of 'MapRoute'...

    Example here: https://our.umbraco.com/Documentation/Reference/Routing/custom-routes-v7#custom-routes-within-the-umbraco-pipeline

    The MapUmbracoRoute needs to have associated an IPublishedContent item...

    I would normally create a ProductsSectionPage in Umbraco and use that

    You can create your own logic for finding that content item with your own VirtualNodeRoteHandler and implementing TryFindContent... Or you can use the one in the example shipped with Umbraco that will find content by id

    With thst in place your controller should inherit RenderMVCController and have an index action that takes a RenderModel content parameter along with your id

    This content item will be the IPublishedContent item you associated in MapUmbracoRoute...

    You can create your own custom view model that inherits from RenderModel and populate it with your products properties using the Id to pull data from your external source...

    Pass your populated view model to your View... And at the top of your view have @inherits UmbracoViewPage<YourViewModel> Your view can then share its master layout with the rest of your site without error...

    Any common implementation from the rest of your site like breadcrumbs will use the ProductsSectionPage as context...

    Regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft