So, I've got my Umbraco site up and running. Things are being pulled from Umbraco's tables that was created, I'm happy with that. My latest challenge is that the site also pulls in data from tables in a database that is NOT a part of Umbraco.
I tried following the instructions in https://our.umbraco.com/documentation/reference/routing/custom-controllers but I can't get it to work. Basically, what I did was create a 'Controllers' folder in my project that has the Umbraco folders in it, and created the controller (based on Umbraco.Web.Mvc.RenderMvcController) for the template in interest (the controller is named for the doc type, action method for the template name).
Then I created the Models folder, added a model (student course) based on a table from the non-Umbraco database (not using a viewmodel just yet). So far, everything seems to be okay. I added a query to the controller that returns a list of student courses via return base.Index(newlist).
Then, per the Umbraco doc, I added @inherits Umbraco.Web.Mvc.UmbracoViewPage<student_cms.Models.vw_get_courses> - this is here I get confused. The @foreach (var item in Models) { query } isn't working; the word Models is red. Says studentcms.Models.vwstudentcourses is not enumerable. This wasn't an issue in the other websites that uses the same query/passing of the list to view (non Umbraco sites).
What am I missing here? I know I'm making all kinds of mistakes here, stil trying to get a grasp how this work.
Here's how I built my model - it's quite simple, pulling in a list of courses from an database, filtering it by the date each course was created (to get the more recent ones) and returning the list to the view.
public class PostsByCategoryLifeController : Umbraco.Web.Mvc.RenderMvcController
{
public ActionResult PostsByStudent(RenderModel model)
{
var db = new CourseModel();
DateTime goBackInTime = DateTime.Today.AddDays(-30);
var thelist = db.vw_get_courses.Where(p => p.EP_D_CREATED > goBackInTime).OrderByDescending(p => p.EP_D_CREATED);
var newcourses = thelist.ToList();
return CurrentTemplate(newcourses);
}
}
The newcourses has a red squiggly line underneath,issue is: cannot convert from System.Collections.Generic.List
To be able to use a view that inherits from UmbracoViewPage, you will need to give it a RenderModel
So the class you give to CurrentTemplate()needs to inherit from RenderModel
public class YourViewModel : RenderModel
{
// Standard Model Pass Through
public YourViewModel (RenderModel baseModel) : base(baseModel.Content, baseModel.CurrentCulture) { }
public List<YourListItemModel> CustomItems{ get; set; } = new List<YourListItemModel>();
}
And then in your controller you do
var viewModel = new YourViewModel(model){
CustomItems = newcourses
}
return CurrentTemplate(viewModel);
Custom model from a non-umbraco database
So, I've got my Umbraco site up and running. Things are being pulled from Umbraco's tables that was created, I'm happy with that. My latest challenge is that the site also pulls in data from tables in a database that is NOT a part of Umbraco.
I tried following the instructions in https://our.umbraco.com/documentation/reference/routing/custom-controllers but I can't get it to work. Basically, what I did was create a 'Controllers' folder in my project that has the Umbraco folders in it, and created the controller (based on Umbraco.Web.Mvc.RenderMvcController) for the template in interest (the controller is named for the doc type, action method for the template name).
Then I created the Models folder, added a model (student course) based on a table from the non-Umbraco database (not using a viewmodel just yet). So far, everything seems to be okay. I added a query to the controller that returns a list of student courses via return base.Index(newlist).
Then, per the Umbraco doc, I added
@inherits Umbraco.Web.Mvc.UmbracoViewPage<student_cms.Models.vw_get_courses>
- this is here I get confused. The @foreach (var item in Models) { query } isn't working; the word Models is red. Says studentcms.Models.vwstudentcourses is not enumerable. This wasn't an issue in the other websites that uses the same query/passing of the list to view (non Umbraco sites).What am I missing here? I know I'm making all kinds of mistakes here, stil trying to get a grasp how this work.
Looks as if you're very close...
Error message is very clear, the vwgetcourses model doesn't implement IEnumerable, so would need more info about this model
Can you share how you've build up the model to return to the view (I guess from what I read it's newlist
From another guess, and assuming your vwgetcourses is already a list of whatever, how about changing the @inherits to
--DIrk
Thank you Dirk for your response!
Here's how I built my model - it's quite simple, pulling in a list of courses from an database, filtering it by the date each course was created (to get the more recent ones) and returning the list to the view.
The newcourses has a red squiggly line underneath,issue is: cannot convert from System.Collections.Generic.List
To be able to use a view that inherits from UmbracoViewPage, you will need to give it a RenderModel
So the class you give to
CurrentTemplate()
needs to inherit from RenderModelAnd then in your controller you do
is working on a reply...