Copied to clipboard

Flag this post as spam?

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


  • Nigel Wilson 939 posts 2061 karma points
    May 20, 2015 @ 03:33
    Nigel Wilson
    0

    Help With Strongly Typed Views for Randomised Output

    Hi there

    Firstly, I am a NOOB to MVC using Umbraco.

    I have been tasked with outputting all nodes of a certain type in a random order, but for that random order to remain consistent for a 24 hour period. To date I have not found an Out-of-the-box way to achieve this with partial views. Does anyone know if this is possible?

     

    A solution I currently have in my head is :

     

    • create a model which only contains the properties I need for my page output, and add a property for a random number
    • within the custom controller create list of my custom model including the "randomisation" logic
    • within the view output the list created by 

     

    So I have scoured Google for some help and did find some resources but have been unable to get anything working as yet.

    My model is as follows:
    -----------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace umb724.Models
    {
        public class PersonModel
        {
            public int ID { get; set; }
            public string FirstName { get; set; }
            public string Surname { get; set; }
            public string URL { get; set; }
        }
    }

     

    My controller is as follows (pseudo entries purely in an attempt to wire up the MVC functionlity (random functionality will be included later):
    -----------------------------------

    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using umb724.Models;
    using Umbraco.Web.Mvc;

    namespace umb724.Controllers
    {
        public class PersonController : SurfaceController
        {
            public ActionResult Person()
            {
                List personModel = new List();                       

                PersonModel pm = new PersonModel();
                pm.FirstName = "Joe";
                pm.Surname = "Bloggs";
                pm.ID = 1234;
                personModel.Add(pm);

                PersonModel pm2 = new PersonModel();
                pm.FirstName = "John";
                pm.Surname = "Doe";
                pm.ID = 4321;
                personModel.Add(pm2);

                return View(personModel);
            }
        }
    }

     

    My View is as follows:
    -----------------------------------
    @model umb724.Models.PersonModel
    @{
        Layout = "~/Views/Master.cshtml";
    }

    @foreach (var item in Model.PersonListing)
    {

    @item.FirstName

     

    }

    ====================================

    Is anyone able to please assist and get me on track with respect to this ?

    Alternatively, can anyone sugest a simpler way to generate a random output that changes based on the day.

    Thanks, Nigel

  • Carl Jackson 139 posts 478 karma points
    May 22, 2015 @ 17:49
    Carl Jackson
    0

    I think your idea is there but you don't have a way of making it ocnsistant for 24 hours?

    How About using the built in umbraco ApplicationCache to cache for 24 hours?

    Entirely untested but something like.....

    public class PersonController : SurfaceController
    {
        public ActionResult Person()
        {
            var model = (List<PersonModel>) ApplicationContext.Current.ApplicationCache.RuntimeCache
                        .GetCacheItem("PersonModelCache", () =>
                        {
    
                            var personModel = new List<PersonModel>();                       
    
                            PersonModel pm = new PersonModel();
                            pm.FirstName = "Joe";
                            pm.Surname = "Bloggs";
                            pm.ID = 1234;
                            personModel.Add(pm);
    
                            PersonModel pm2 = new PersonModel();
                            pm.FirstName = "John";
                            pm.Surname = "Doe";
                            pm.ID = 4321;
                            personModel.Add(pm2);
                            return personModel
                        }, TimeSpan.FromMinutes(1440));
    
            return View(model);     
        }
    
    }
    
  • Nigel Wilson 939 posts 2061 karma points
    May 22, 2015 @ 20:07
    Nigel Wilson
    0

    Hi Carl

    Thanks for your great suggestion re caching - I had been wondering about this aspect so very timely.

    The randomisation aspect I haven't worked out yet but have some ideas in my head - my initial pain point is in getting the MVC part to work but you my have solved that in your code too.

    I will have a play with your suggestion and see how I get on.

    Again, thanks for the feedback - truly appreciated.

    Regards

    Nigel

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies