Copied to clipboard

Flag this post as spam?

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


  • Linx 32 posts 122 karma points
    1 week ago
    Linx
    0

    How to return List without document type

    Hi all

    Relatively new to Umbraco 8.

    I have a list of customers. I want to be able to retrieve them from a database (which I can use some existing logic).

    Once retrieved, I want to be able to display them in an Umbraco page.

    I have a document type and a standard template and add a partial view to this template.

    I'm inheriting from RenderMvcController in my controller but every time I add this View I keep receiving casting errors.

    Is it possible to have a separate View getting data from my database in this manner?

    Thanks for any help

  • Marc Goodson 1718 posts 11411 karma points MVP 7x c-trib
    1 week ago
    Marc Goodson
    2

    Hi Linx

    There are several different approaches you can use in this scenario, and the best approach depends on the site and what you are ultimately trying to achieve but you could...

    Just listing customers

    a) If you are just listing out the customers on a page, then if you create the page in Umbraco, in the template you could have an Umbraco SurfaceController child action, that goes off to retrieve the customers and return a partial view that will be displayed within the Umbraco Template. https://our.umbraco.com/Documentation/Reference/Routing/Surface-Controllers/index-v8

    b) You could use 'RouteHijacking' which is the creation of an Umbraco RenderMvcController following a specific naming convention, the controller matches the alias of the Document Type you want to hijack. If you had a CustomerListing DocumentType then a CustomerListingRenderMvcController that inherited from RenderMvcController would allow you to intercept the request, and build up your own custom ViewModel (inheriting from Umbraco's ContentModel) that could have a property for the Customer List that you populate in your hijacked controller. https://our.umbraco.com/Documentation/Reference/Routing/Custom-Controllers/index-v8

    Virtual Page for each Customer

    Or if you want each customer to have their own page, without having to create them in Umbraco, this can be achieved in a couple of different ways

    i) Map a Custom Umbraco Route using 'MapUmbracoRoute' this would allow you to specify a route such as "customers/{id}" (or similar depending how you can lookup the customer) - and map this to a Custom RenderMvcController (doesn't have to follow a naming convention this time) - however in the MapUmbracoRoute registration you have to tell Umbraco 'which' Umbraco Published Content item to associate with the route (this means your views that have breadcrumbs in etc, still work base on the associated IPublishedContent object). You would then in your RenderMvcController read the id, look up the customer, add it to a CustomViewModel and then send it to your view to display the customer details on their own page! https://our.umbraco.com/Documentation/Reference/Routing/Custom-Routes/index-v8

    ii) If the 'route' isn't always the same, then you can register a custom step in the Umbraco content finding pipeline (a series of finders that try to match the incoming URL to an IPublishedContent item). eg if your site had lots of published region pages, you might want to show customers underneath the region, and so your Custom Content finder, would look at the URL /customers/australia/dave-smith and would find in umbraco the published region called Australia, and then look in your customers database for dave-smith, and then construct an object outside of Umbraco that implements IPublishedContent, and then it would appear exactly as if the Customer was created in Umbraco...https://our.umbraco.com/Documentation/Reference/Routing/Request-Pipeline/IContentFinder-v8

    Anyway hard to explain the complication and subtlety between the different options and how they might suit your situation, but hopefully that gives you a glimpse of what might be possible.

    regards

    Marc

  • Linx 32 posts 122 karma points
    1 week ago
    Linx
    0

    Hi @Marc

    That's really really helpful! Clears up a lot of confusion with your explanation.

    I'm going to try this out and feedback.

    Many thanks again

  • Linx 32 posts 122 karma points
    1 week ago
    Linx
    0

    Hi @Marc

    I went through the links you provided and did a bit of digging around to get this to work.

    I create a new Controller with two methods

    CustomerController

        public ActionResult Indexed()
        {
          var customers = getSomeCusomers();
    
          return View(customers);
        }
    
        public ActionResult Render()
        {
            var testList = new List<Customer>();
    
            return PartialView("TestList", testList);
    
        }
    

    I know the above code is a little messy but trying to get my grip here after multiple changes.

    I create a template in Umbraco which contains the key line

    @Html.Action("Render","Customer")
    

    The view displays as needed.

    I create a new PartialView (within the Partial View folder) and add

    @model IEnumerable<MyClassLibrary.Customers.Models.CustomerModel>
    
    Customer list
    
    @using (Html.BeginUmbracoForm("Indexed", "Customer"))
    {
        foreach (var i in Model)
        {
            <p>@i.Name</p>
        }
    }
    

    But this method never hits. Not sure what i may have done wrong?

  • Marc Goodson 1718 posts 11411 karma points MVP 7x c-trib
    1 week ago
    Marc Goodson
    0

    Hi Linx

    So are you just trying to list out the customers or are you trying to make a form that posts back to a controller?

    BeginUmbracoForm helps you wire up a form to post back to a SurfaceController, but if you just want to list out customers then you only need a ChildAction that builds the model with the customers in and a partial view to loop through each customer to display the name...

    ... Or I maybe totally misunderstanding!

    Regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft