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
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???
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>
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!
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!
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???
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!
Thanks Graeme, will give that a go - cheers
I am new to umbraco, may I know the steps of access the external database in umbraco?
is working on a reply...