Skip to content

Commit d997248

Browse files
authored
Add example for loading submitted form records
Added sample code for loading a record from a submitted form using FormService and RecordStorage.
1 parent b0ee4ed commit d997248

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

13/umbraco-forms/developer/working-with-data.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,45 @@ Sample script that is outputting comments using a Form created with the default
110110
}
111111
</ul>
112112
```
113+
114+
115+
## Loading a Record From a Submitted Form
116+
When a form is submitted, the submitted form id and the saved record id is stored in the TempData so it can be referenced.
117+
118+
You can use the FormService and the RecordStorage to get the ```Form``` and ```Record``` objects.
119+
120+
Here is sample code for how to get the record in a view.
121+
122+
```
123+
@using Umbraco.Forms.Core.Models
124+
@using Umbraco.Forms.Core.Persistence.Dtos
125+
@using Umbraco.Forms.Core.Data.Storage
126+
@using Umbraco.Forms.Core.Services
127+
@inject IFormService _formService
128+
@inject IRecordStorage _recordStorage
129+
@inherits UmbracoViewPage
130+
@{
131+
Guid formId;
132+
Form? form;
133+
Guid recordId;
134+
Record? record;
135+
string submittedEmail;
136+
137+
if (TempData["UmbracoFormSubmitted"] != null)
138+
{
139+
Guid.TryParse(TempData["UmbracoFormSubmitted"]?.ToString(), out formId);
140+
141+
form = _formService.Get(formId);
142+
143+
if (form != null && TempData["Forms_Current_Record_id"] != null)
144+
{
145+
Guid.TryParse(TempData["Forms_Current_Record_id"]?.ToString(), out recordId);
146+
147+
record = _recordStorage.GetRecordByUniqueId(recordId, form);
148+
149+
submittedEmail = record.GetRecordFieldByAlias("email")?.ValuesAsString();
150+
}
151+
}
152+
}
153+
```
154+

0 commit comments

Comments
 (0)