Copied to clipboard

Flag this post as spam?

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


  • Nancy A. 44 posts 175 karma points
    Feb 15, 2019 @ 22:34
    Nancy A.
    0

    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.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 16, 2019 @ 07:30
    Dirk De Grave
    0

    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

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<puclife_cms.Models.vw_get_courses>>
    

    --DIrk

  • Nancy A. 44 posts 175 karma points
    Feb 19, 2019 @ 19:27
    Nancy A.
    0

    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.

    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

  • Sven Geusens 169 posts 881 karma points c-trib
    Feb 20, 2019 @ 11:18
    Sven Geusens
    0

    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);
    
Please Sign in or register to post replies

Write your reply to:

Draft