Copied to clipboard

Flag this post as spam?

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


  • Graeme W 113 posts 289 karma points
    Apr 07, 2014 @ 12:46
    Graeme W
    0

    Accessing external database from partial view macro

    Can anyone advise the best way of displaying external data using a partial view macro?

    I need to get data from an external database into my Umbraco (v6 MVC) website. I’ve done something similar with surface controllers which are being fired from a partial view in a template. However I now have a requirement to show data from an external table the view of which will differ depending on which page it’s called from. I think a macro is the way to go as I don’t want to have to create a template for each page and I can use parameters

    Thanks!

  • Graeme W 113 posts 289 karma points
    Apr 08, 2014 @ 15:16
    Graeme W
    0

    Have achieved my goal by adding a @function block at the top of the view.

    Just takes a couple of lines of code to get the macro parameter and then get a list of objects (using peta poco) which I can use in my razor view.

    Not sure if this is the "right" way to do it but it works!

  • David Tregoning 63 posts 236 karma points
    Sep 24, 2014 @ 10:28
    David Tregoning
    0

    Hi Graeme, This is exactly what I would like to do, do you have examples of the Model, Controller cs files as I am new to MVC and not sure which route I should be taking to achieve this, eg.g PetaPoco???

  • Graeme W 113 posts 289 karma points
    Sep 24, 2014 @ 13:58
    Graeme W
    0

    Hi David.

    I put my code directly in a template in the end as the macro was confusing for the content editors. Yes I used PetaPoco and the simplest way was to embed the code in the top of the template in a function block. Then call the function, loop through the objects in the razor code to render the content. As I said above not sure if it's the "right" way to do it.

    The code below is pasted from a partial view called from the template that queries another database (not the Umbraco db). Note that: - the staff object needs to be setup in the models folder. - myDb needs to be a connect string in your web.config - in this case CategoryID is a property of the containing page's document type which is entered to match the relevant value on the db Hope this helps!

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using LPPUmbracoMVCWebApp.Models
    
    @using Umbraco.Core.Persistence; @*for peta poco *@
    
    @functions {   
    
        public List<Staff> GetStaff(string myCat){
         var db = new Database("myDB");
          return db.Query<LPP_Staff>("SELECT * FROM StaffDetails WHERE CategoryID = " + myCat + " ORDER BY Surname").ToList();  
    
        }              
    }
    
    @{      
        // value from the containing view's model...
        string catRef = Model.Content.GetPropertyValue("CategoryID").ToString();
        var myStaff = GetStaff(catRef);    
    }
    
    <h2>Our Staff</h2>
        <table class="table table-bordered"><tr><th>Name</th><th>Title</th><th>Telephone</th><th>EMail</th></tr>
        @foreach (Staff s in myStaff)
     {        
    
      <tr>
    
      <td>
      @s.FirstName @s.Surname
         </td>
         <td>@s.JobDescription</td>
         <td>@s.TelephoneNo  
    
    
         </td>
         <td>
          @s.LoweredEmail
         </td>
         </tr>
    
     } 
    
        </table>
    
  • David Tregoning 63 posts 236 karma points
    Sep 26, 2014 @ 13:29
    David Tregoning
    0

    Thanks Graeme, will give that a go - cheers

  • john 1 post 71 karma points
    Jun 28, 2016 @ 03:06
    john
    0

    I am new to umbraco, may I know the steps of access the external database in umbraco?

Please Sign in or register to post replies

Write your reply to:

Draft