Copied to clipboard

Flag this post as spam?

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


  • Douglas Ludlow 210 posts 366 karma points
    Jun 18, 2012 @ 19:37
    Douglas Ludlow
    0

    Sort and iterate through form fields with Razor?

    I have a form with 50+ fields and I want to be able to display the field values of a submitted record on a page using razor without having to type the caption and value of each field. I have the following so far.

    Contour.Addons.DynamicObjects.DynamicRecord record = Contour.Addons.DynamicObjects.Library.GetRecordsFromForm("216B068C-2C42-42F0-91D4-F4E24AB82A84")
    .Cast<Contour.Addons.DynamicObjects.DynamicRecord>()
    .Where(r => r.Id.ToUpper() == "28EDEBD8-3705-48D8-A70A-EC3EF5D03BC0".ToUpper())
    .FirstOrDefault();

    if (record != null)
    {
    <span>Id: @record.Id </span><br />
    <span>Updated: @record.Updated </span><br />
    <ul>
    @foreach (var field in record.RecordFields)
    {
    <li>
    <b>@field.Value.Field.Caption</b><p>@field.Value.ValuesAsString()</p>
    <b>Page:</b><p>@field.Value.Field.PageIndex</p>
    <b>Fieldset:</b><p>@field.Value.Field.FieldsetIndex</p>
    <b>Fieldset:</b><p>@field.Value.Field.SortOrder</p>
    </li>
    }
    </ul>
    }
    }

    Which shows all of the record fields just fine. At this point I would like to sort the fields by page, fieldset, and sort order, but I am unable to do so, however, because the PageIndex, FieldsetIndex, and SortOrder properties all return 0 for each record field.

    Is it possible to get the source of Contour.Addons.DynamicObjects or are there any updates available to it since it was posted last year?

    I've tried to get at a record a few different ways, but I haven't been able to figure out how to instantiate a Record outside of a workflow type...

  • Douglas Ludlow 210 posts 366 karma points
    Jun 18, 2012 @ 20:20
    Douglas Ludlow
    0

    Blurg... I guess it's not a problem with Contour.Addons.DynamicObjects. Using RecordsViewer, I get the exact same results:

    Form form = new Form { Id = new Guid("216B068C-2C42-42F0-91D4-F4E24AB82A84") };

    RecordsViewer rv = new RecordsViewer();
    Record record = rv.GetRecords(form).FirstOrDefault(r => r.Id == new Guid("28EDEBD8-3705-48D8-A70A-EC3EF5D03BC0"));

    if (record != null)
    {
    <span>Id: @record.Id </span><br />
    <span>Updated: @record.Updated </span><br />
    <ul>
    @foreach (var field in record.RecordFields)
    {
    <li>
    <b>@field.Value.Field.Caption</b><p>@field.Value.ValuesAsString()</p>
    <b>Page:</b><p>@field.Value.Field.PageIndex</p>
    <b>Fieldset:</b><p>@field.Value.Field.FieldsetIndex</p>
    <b>Fieldset:</b><p>@field.Value.Field.SortOrder</p>
    </li>
    }
    </ul>
    }

    Any ideas on how to sort these guys in order of appearance on the form?

  • Douglas Ludlow 210 posts 366 karma points
    Jun 18, 2012 @ 21:26
    Douglas Ludlow
    2

    Got it figured out. I just needed to use RecordStorage:

    RecordStorage storage = new RecordStorage();
    Record record = storage.GetRecord(new Guid("28EDEBD8-3705-48D8-A70A-EC3EF5D03BC0"));
    RecordService service = new RecordService(record);

    string pageName = service.Form.Pages[0].Caption;
    string fieldsetName = service.Form.Pages[0].FieldSets[0].Caption;

    var recordFields = record.RecordFields.OrderBy(r => r.Value.Field.PageIndex).ThenBy(r => r.Value.Field.FieldsetIndex).ThenBy(r => r.Value.Field.SortOrder);

    <h2>@pageName</h2>
    <span>Id: @record.Id </span><br />
    <span>Updated: @record.Updated </span><br />
    <h3>@fieldsetName</h3>
    <ul>
    @foreach (var field in recordFields)
    {
    if (pageName != service.Form.Pages[field.Value.Field.PageIndex].Caption || fieldsetName != service.Form.Pages[field.Value.Field.PageIndex].FieldSets[field.Value.Field.FieldsetIndex].Caption)
    {
    pageName = service.Form.Pages[field.Value.Field.PageIndex].Caption;
    fieldsetName = service.Form.Pages[field.Value.Field.PageIndex].FieldSets[field.Value.Field.FieldsetIndex].Caption;

    @:</ul>
    <h2>@pageName</h2>
    <h3>@fieldsetName</h3>
    @:<ul>
    }

    <li>
    <b>@field.Value.Field.Caption</b><p>@field.Value.ValuesAsString()</p>
    </li>
    }
    </ul>
  • Douglas Ludlow 210 posts 366 karma points
    Jun 18, 2012 @ 21:29
    Douglas Ludlow
    0

    Note: I had the following using statements:

    @using Umbraco.Forms.Core
    @using Umbraco.Forms.Core.Services
    @using Umbraco.Forms.Data.Storage
  • Douglas Ludlow 210 posts 366 karma points
    Jun 18, 2012 @ 21:40
    Douglas Ludlow
    3

    Final:

    @using Umbraco.Forms.Core
    @using Umbraco.Forms.Core.Services
    @using Umbraco.Forms.Data.Storage
    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @try
    {
    /*
    * A macro that shows a submitted form record.
    * Author: Douglas Ludlow
    * Created: 06/18/2012
    *
    * Updates:
    */

    RecordStorage storage = new RecordStorage();
    Record record = storage.GetRecord(new Guid("28EDEBD8-3705-48D8-A70A-EC3EF5D03BC0"));
    RecordService service = new RecordService(record);

    var page = service.Form.Pages[0];
    var fieldset = page.FieldSets[0];
    var recordFields = record.RecordFields.OrderBy(r => r.Value.Field.PageIndex).ThenBy(r => r.Value.Field.FieldsetIndex).ThenBy(r => r.Value.Field.SortOrder);

    <h2>@page.Caption</h2>
    <p>
    <span>Id: @record.Id </span><br />
    <span>Updated: @record.Updated </span><br />
    </p>
    <h3>@fieldset.Caption</h3>
    <ul>
    @foreach (var field in recordFields)
    {
    if (page.SortOrder != field.Value.Field.PageIndex || fieldset.SortOrder != field.Value.Field.FieldsetIndex)
    {
    page = service.Form.Pages[field.Value.Field.PageIndex];
    fieldset = page.FieldSets[field.Value.Field.FieldsetIndex];

    @:</ul>
    <h2>@page.Caption</h2>
    <h3>@fieldset.Caption</h3>
    @:<ul>
    }

    <li>
    <b>@field.Value.Field.Caption</b><p>@field.Value.ValuesAsString()</p>
    </li>
    }
    </ul>
    }
    catch (Exception e)
    {
    <!-- @e.ToString() -->
    }
Please Sign in or register to post replies

Write your reply to:

Draft