Skip to content

Commit d081f0a

Browse files
authored
Merge pull request #7381 from kmeilander/patch-10
Document loading records from submitted forms
2 parents 6e05b92 + 5714355 commit d081f0a

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-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 are stored in the `TempData` so they can be referenced.
117+
118+
You can use the FormService and the RecordStorage to get the `Form` and `Record` objects.
119+
120+
Here is a sample code for retrieving a 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 (Guid.TryParse(TempData["UmbracoFormSubmitted"]?.ToString(), out Guid formId) &&
138+
Guid.TryParse(TempData["Forms_Current_Record_id"]?.ToString(), out Guid recordId))
139+
{
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+

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

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

0 commit comments

Comments
 (0)