Conent Nodes saved to different members

83 views Asked by At

I've posted this quesiton in the Umbraco Forums as well as the Umbraco Discord without getting a response so I thought I'd try here as well :)

I've managed to store different fields from a specific form in the backoffice with the Content Node workflow.

Those content nodes are school classes, and I want the users to be able to toggle between the classes on the page itself. Preferably its bound to the member so they could swap between school classes they themselves registered. Apparently the UmbracoFormsStorage is no longer available in the latest versions for some reason.

Anyone know how to do this in a Razor view in Umbraco 11? Or do I have to setup controllers and use the service APIs / GUID?

I've been trying to use @using Umbraco.Forms.Data.Storage; FormStorage and RecordStorage but it doesn't seem to work on Umbraco 11. I've also tried the request method and HttpContextAccessor.HttpContext.Request.Form.ContainsKey("myField"))

But none of them works. Any idea how to solve this? I'd be happy to share some of the code for y'all to get an idea on what I'm trying to achieve.

@inherits UmbracoViewPage<SiteBuilderBaseViewModel>
@using USNSiteBuilder.Core.Models
@using Umbraco.Cms.Core.Security
@using Microsoft.AspNetCore.Http
@using Umbraco.Forms.Core.Models
@using Umbraco.Forms.Core.Services;
@using Newtonsoft.Json.Linq;
@using Umbraco.Forms.Web.Services;
@using Microsoft.AspNetCore.Http.Features;
@using Umbraco.Forms.Data.Storage;
@using Microsoft.AspNetCore.Mvc.Rendering
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
@inject IMemberManager MemberManager

@{
    Layout = "USNMaster.cshtml";
    Usnstyle websiteStyle = (Usnstyle)Model.WebsiteStyle;

    var currentMember = await MemberManager.GetCurrentMemberAsync();

    if (currentMember != null)
    {
        var formId = "umbraco_form_2234732813ed4ada88a0370f2630e351"; // replace with your form ID
        var storage = new UmbracoFormsStorage();
        var form = storage.GetForm(Guid.Parse(formId));
        var registeredClasses = form.AllFieldValues.ContainsKey("registeredClasses")
            ? form.AllFieldValues["registeredClasses"].ToString()
            : "";
        var classIds = registeredClasses.Split(',');
        var classNodes = new List<IPublishedContent>();

        foreach (var classId in classIds)
        {
            var classNode = Umbraco.Content(classId);
            if (classNode != null)
            {
                classNodes.Add(classNode);
            }
        }

        foreach (var classNode in classNodes)
        {
            <div>
                <h3>@(classNode.Value<string>("klassensNamn"))</h3>
                <p>@(classNode.Value<string>("aarskull"))</p>
                <p>@(classNode.Value<string>("skolansNamn"))</p>
                <p>@(classNode.Value<string>("skolansAdress"))</p>
                <p>@(classNode.Value<string>("skolansPostnummer")) @(classNode.Value<string>("skolansPostort"))</p>
                <p>@(classNode.Value<string>("antalElever"))</p>
                <p>@(classNode.Value<string>("pedagogensNamn"))</p>
            </div>
        }
    }
}

1

There are 1 answers

0
dampee On

If you are handling forms, there are a few ways to do this.

If you are not using Umbraco forms, but building (and storing) your own form, you should use a SurfaceController. This enables you to use [HttpPost] to react to the form submission instead of writing code in razor.

Second, If you ARE using umbraco forms, you probably want to create a custom field type or a custom workflow to be able to store other values. Make sure you read the extending Umbraco Forms section from the documentation.