Copied to clipboard

Flag this post as spam?

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


  • Kris Janssen 210 posts 569 karma points c-trib
    Nov 19, 2013 @ 00:01
    Kris Janssen
    0

    Get record info using Razor

    I have a Contour form set up which, among other things, allows someone to submit an Email address.

    The caption for the form field is "Email".

    Based on all submitted forms I'd like to present a page on the website displaying all submitted records but this yields an error message...

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @using Umbraco.Forms.Mvc.DynamicObjects;
    
    <ul id="comments"> 
    @foreach (dynamic record in Umbraco.Forms.Mvc.DynamicObjects.Library 
           .GetApprovedRecordsFromPage(1157)) 
    { 
     <li> 
         @record.Email.ToString();
     </li> 
    } 
    

    Error Loading Razor Script (file: [RAZ] Applications) Sequence contains no elements
    at System.Linq.Enumerable.First[TSource](IEnumerable`1 source) at Umbraco.Forms.Mvc.DynamicObjects.DynamicRecord.TryGetMember(GetMemberBinder binder, Object& result) c:\Program Files >(x86)\teamcity\buildAgent\work\fdc2f0fcbbcef310\Umbraco.Forms.Mvc \DynamicObjects\DynamicRecord.cs:line 63 at CallSite.Target(Closure , CallSite , Object )

    Strangely, following returns "true":

    @record.HasField("Email");
    

    Then again:

    @record.HasField("SomethingRandom");
    

    Also returns true, even if "SomethingRandom" is not a field on the form (and should therefore not be a property of the record).

    This is rather urgent, does anybody have an idea??

    Cheers,

    Kris

  • Kris Janssen 210 posts 569 karma points c-trib
    Nov 20, 2013 @ 18:17
    Kris Janssen
    0

    Anybody have an idea as to why the dot notation does not seem to work as described above?

    I found an alternative that does work though:

    @foreach (dynamic record in Umbraco.Forms.Mvc.DynamicObjects.Library.GetRecordsFromForm("c3682d05-716a-48bf-9348-790339692999")) 
    { 
           <tr> 
                                                           @foreach (var field in record.RecordFields)
                 {
                                                       if(@field.Value.Field.Caption.Contains("First Name"))
                                                       {
                                                    <td>@field.Value.ValuesAsString()</td>
                                                       }
    

    But still, I would expect the approach described here t also work...

  • Comment author was deleted

    Nov 22, 2013 @ 11:05

    No can you show me the caption of the field that doesn't work, then I can test :)

  • Kris Janssen 210 posts 569 karma points c-trib
    Nov 22, 2013 @ 21:27
    Kris Janssen
    0

    Hi Tim,

    In hindsight, I think the issues I described here are related to my using quite a few non-alphanumeric characters and Trygetmember() not cleaning things up enough.

    If you want to get multiple fields of a record (e.g. when populating several 's in a table with record data, the exception being thrown is quite cryptic, mapmaking me think things were not working at all whereas now, I noticed record.Email would work while record.CVPDF would not work for a caption which was originally "CV (PDF)" ith the brackets being the culprit here.

    Captions such as:

    Position (1st) CV (PDF) ...

  • Tom 713 posts 954 karma points
    Oct 13, 2014 @ 08:08
    Tom
    0

    Just wondering how you ending up dealing with the brackets as I'm using a description of File Upload (max size 2mb)

    Thanks

  • Kris Janssen 210 posts 569 karma points c-trib
    Oct 13, 2014 @ 09:26
    Kris Janssen
    0

    Hi,

    As far s I can tell (I last checked the .21 version of contour) this is still not entirely resolved as there is a small error in the reject that is used to clean up the caption in the dynamic implementation.

    I have resorted to using non-dynamic calls to get to the data I need.

    You can do stuff like:

    ...

    RecordStorage rsStorage = new RecordStorage();
    
    // In some cases (many records) I found it to be fastest to work with the guide with which certain fields are stored in records. 
    // Not the most practical but it you do not need to do it often...
    const string fileF = "430e0b11-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
    Guid fileG = new Guid(fileF);
    
    ...
    
    @{
                    int i = 0;
                    IEnumerable<Record> lRecs = rsStorage.GetAllRecords(new Guid("cca978e7--xxxx-xxxx-xxxx-xxxxxxxxxxxx")).Where(r => r.UmbracoPageId == Model.Id);
                }
    
                @if (lRecs.Any())
                {
    
                    foreach (var record in rsStorage.GetAllRecords(new Guid("cca978e7-xxxx-xxxx-xxxx-xxxxxxxxxxxx")).Where(r => r.UmbracoPageId == Model.Id).OrderBy(r => r.Created))
                    {
                        record.UmbracoPageId.ToString();
                        <tr @New(record.Created)>
                            <td><a href="@("http://www.myurl.com" + @record.GetRecordField(fileG).ValuesAsString())">Download</a></td>
                            <td>@CleanString(@strCreated)</td>
                        </tr>
                        i++;
    
                    }
                }
    

    That snippet actually generates a table containing uploaded files.

    There are some more threads about this around here.

    I'll try and have a look at the latest nightly also...

  • Tom 713 posts 954 karma points
    Oct 16, 2014 @ 01:24
    Tom
    0

    how are you guys handling the sequence contains no elements exceptions all over the place. using dynamics or DynamicRecord are you wrapping each field in a try catch?

     

    seems pretty messy

  • Scott 95 posts 277 karma points
    Sep 22, 2015 @ 10:06
    Scott
    100

    Well the easiest way I have found is to do like this:

    @foreach (dynamic record in Library.GetRecordsFromForm("FORM GUID"))
    {
       <td>@record.RecordFields[new Guid("FIELD GUID")].ValuesAsString()</td>
    }
    

    Regards, Scott

  • aaeda 117 posts 150 karma points
    May 23, 2018 @ 11:41
    aaeda
    0

    Hello

    I have tried the above code, but for me it is generating an error message (see below). Any ideas?

    There is already an open DataReader associated with this Command which must be closed first.
    

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    thanks

  • Scott 95 posts 277 karma points
    May 23, 2018 @ 11:45
    Scott
    0

    Hi Aaeda

    Please share a bit of code.

    Kind regards, Scott

  • Scott 95 posts 277 karma points
    May 23, 2018 @ 11:47
    Scott
    0

    Could you post the whole script?

  • aaeda 117 posts 150 karma points
    May 23, 2018 @ 11:46
    aaeda
    0

    Hello Scott

    Here it is

     @foreach (dynamic record in Library.GetRecordsFromForm("03928f28-cceb-4eb4-8eb2-4c1f69927430"))
    
        {
            <li>
                @record.Created.ToString("dd MMMM yyy")
                @if (string.IsNullOrEmpty(record.Website))
                {
                    <strong>@record.Name</strong>
                }
                else
                {
                    <strong>
                        <a href="@record.Website" target="_blank">@record.Name</a>
                    </strong>
                }
                <span>said</span>
                <p>@record.Comment</p>
            </li>
        }
    
  • aaeda 117 posts 150 karma points
    May 23, 2018 @ 11:47
    aaeda
    0

    getting the error message on the 'foreach' line

  • Scott 95 posts 277 karma points
    May 23, 2018 @ 11:48
    Scott
    0

    Oh and also, which version do you use of umbraco and forms

  • aaeda 117 posts 150 karma points
    May 23, 2018 @ 11:50
    aaeda
    0

    Oh yes, it is umbraco 7.7.8 and forms 6.0.7

  • Scott 95 posts 277 karma points
    May 23, 2018 @ 12:10
    Scott
    0

    I'm sorry but could you post the whole script so we can see context please

  • aaeda 117 posts 150 karma points
    May 23, 2018 @ 12:12
    aaeda
    0

    Ok, here it is

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    

    @using createsend_dotnet; @using Umbraco.Forms.Mvc.DynamicObjects

    @{

          @foreach (dynamic record in Library.GetRecordsFromForm("03928f28-cceb-4eb4-8eb2-4c1f69927430"))
      
          {
              <li>
                  @record.Created.ToString("dd MMMM yyy")
                  @if (string.IsNullOrEmpty(record.Website))
                  {
                      <strong>@record.Name</strong>
                  }
                  else
                  {
                      <strong>
                          <a href="@record.Website" target="_blank">@record.Name</a>
                      </strong>
                  }
                  <span>said</span>
                  <p>@record.Comment</p>
              </li>
          }
      </ul>
      

      }

  • Scott 95 posts 277 karma points
    May 23, 2018 @ 12:22
    Scott
    0

    Could you try for a moment and remove the @using createsend_dotnet; part..

    I have tested it without and found it working.

  • aaeda 117 posts 150 karma points
    May 24, 2018 @ 06:51
    aaeda
    0

    hello

    thanks for your help. but I am still getting the error even after removing the create_dotsend. Any idea why?

    thanks

  • Scott 95 posts 277 karma points
    May 24, 2018 @ 07:04
    Scott
    0

    I’ll see if I have some time today to take a look at it in a new environment. it states that you have another datareader open which might be version related. so I will try in an identical setup. Meanwhile you could try to come around it by making a new simple form with just one field. a new view with macro and just put a simple version of your script in it for testing purposes and slowly build it up. So only inheriting and importing what you must and nothing else.

    Take the following script and see if it works, if it does you know it’s something else:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Forms.Mvc.DynamicObjects
    @{
            foreach (dynamic record in Library.GetRecordsFromForm("03928f28-cceb-4eb4-8eb2-4c1f69927430"))
            {
            }
    }
    

    Scott

  • aaeda 117 posts 150 karma points
    May 24, 2018 @ 07:06
    aaeda
    0

    Ok thanks a lot. I will try that.

Please Sign in or register to post replies

Write your reply to:

Draft