Copied to clipboard

Flag this post as spam?

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


  • Ben McKean 272 posts 549 karma points
    Feb 06, 2013 @ 18:00
    Ben McKean
    0

    Creating a poll using Razor

    Hi 

    Has anybody had any experience of creating a poll in Contour and using Razor for users to vote and see results?

    I've seen the example here http://www.nibble.be/?p=81 but this is XSLT. I think I could adapt this but wanted to see if anybody had done anything similar before?

    Thanks!

    Ben

  • Comment author was deleted

    Feb 06, 2013 @ 19:06

    Hi Ben,

    Since COntour v3 there is a razor render of the form so you should be able to use that voting

    TO display the results, that should be even easier then xslt, just change the code for the xslt extension (didn't test this just changed it here without testing)

         @{
    FormStorage fs = new FormStorage(); Form f = fs.GetForm(new Guid(guid)); fs.Dispose(); } @foreach (Field fld in f.AllFields) { if (fld.FieldType.SupportsPrevalues && fld.PreValueSource.Type.GetPreValues(fld).Count > 0) {
    <h3>fld.Caption</h3> <ul> @foreach (PreValue pv in fld.PreValueSource.Type.GetPreValues(fld)) {
    <li>pv.Value</li> }
    </ul> } }

    You'll just need some extra code to calc the amount of times an option is chosen 

  • Ben McKean 272 posts 549 karma points
    Feb 07, 2013 @ 16:15
    Ben McKean
    0
  • Ben McKean 272 posts 549 karma points
    Feb 07, 2013 @ 16:47
    Ben McKean
    0

    Hi Tim

    Thanks for the reply. I've got that working now so a poll is displaying when you've not already voted but doesn't displays when you have. I've got it creating and checking for the cookie.

    I've tweaked your code above so it outputs the options but how do I get number of votes each option has? In your XSLT code you use "umbraco.contour:GetRecordsFromForm" but I'm unsure of the Razor equivalent. I'm guessing I need to pass in the PreValue in the for loop into a method to get the number of results?

    Thanks

    Ben

  • Comment author was deleted

    Feb 07, 2013 @ 17:04

    The same method is available from razor and returns dynamic objects

    Umbraco.Forms.Mvc.DynamicObjects.Library

    http://our.umbraco.org/projects/umbraco-pro/contour/documentation/Developer/Working-with-Contour-data-in-Razor/

    You should then be able to use a where clause like is available on dynamicnode in umbraco

    Don't know the syntax by heart but I'll give it a try

  • Comment author was deleted

    Feb 07, 2013 @ 17:27

    Ok try the following:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using Umbraco.Forms.Core
    @using Umbraco.Forms.Data.Storage
    
    
         @{
    FormStorage fs = new FormStorage();
    
                Form f = fs.GetForm(new Guid("f6b912c0-7921-42d7-bda0-7be025f72d45"));
                fs.Dispose();
    
              RecordStorage r = new RecordStorage();
                var records = r.GetAllRecords(new Guid("f6b912c0-7921-42d7-bda0-7be025f72d45"));
                r.Dispose();
         }
                @foreach (Field fld in f.AllFields)
                {
    
                    if (fld.FieldType.SupportsPrevalues 
                        && fld.PreValueSource.Type.GetPreValues(fld).Count > 0)
                    {
        <h3>@fld.Caption</h3>
         <ul>
                        @foreach (PreValue pv in fld.PreValueSource.Type.GetPreValues(fld))
                        {
                            var count = records.Where(rec => rec.RecordFields.FirstOrDefault().Value.ValuesAsString() == pv.Value).Count();
    
    
    <li>@pv.Value - @count</li>
                        }
                </ul>
    
                    }
    
                }
  • Comment author was deleted

    Feb 07, 2013 @ 17:28

    It just adds the amount of times an option has been voted for, to add percentage count the total ammount of records first ... 

  • Ben McKean 272 posts 549 karma points
    Feb 07, 2013 @ 17:35
    Ben McKean
    0

    I was coding the same but your method looks a lot slicker than the route mine was going.

    Thank you so much for your help, works perfectly! I'll finish off the percentages etc.

  • Comment author was deleted

    Feb 07, 2013 @ 17:36

    No prob, glad it works now :)

Please Sign in or register to post replies

Write your reply to:

Draft