Working with Form Data from Code Behind
I was attempting to retrieve data from the Forms module in code behind. I "invented" this technique because I could not see a way to get the data strongly typed. If there is a way to do this strongly typed it would be far superior to using dynamic. Wanted to get your thoughts on this approach (i.e. using the .NET "dynamic" keyword). Please advise. . . .
01.
var formObjects = App.WorkWith().Forms().Form(
this
.FormName).Entries().Get()
02.
.ToArray()
03.
.Cast<dynamic>()
04.
.Select(x =>
new
Location()
05.
06.
Email = x.FormTextBox_4,
07.
Phone = x.FormTextBox_0,
08.
Name = x.FormTextBox_1,
09.
Address = x.FormTextBox_2,
10.
LatLong = x.FormTextBox_3
11.
);
Hi Shawn Weisfeld,
Thank you for using our services.
You cannot use strongly typed property names with dynamic fields. What you can do is to set field names for each text box when you add it to your form, see attached image. Then from code you will be able to use this field name to get (GetValue()) or set value (SetValue()):
using
Telerik.Sitefinity.Model;
...
var formObjects = App.WorkWith().Forms().Form(
this
.FormName).Entries().Get()
.ToArray()
.Select(x =>
new
Location()
Email = x.GetValue(
"EmailFieldName"
),
Phone = x.GetValue(
"PhoneFieldName"
),
Name = x.GetValue(
"NameFieldName"
),
Address = x.GetValue(
"AddressFieldName"
),
LatLong = x.GetValue(
"LatLongFieldName"
)
);
Hi,
How do I get all the form fields dynamically. In this case you know the fields. In my case I donot know the fields. There can be 1 field or 10 fields. How do I get all the fields in a form.
Thanks
Madhavan
Hi Madhavan,
You can use TypeDescriptor.GetProperties Method
Kind regards,
Ivan Dimitrov
the Telerik team