Access value enumeration field fom content part in Editor Method Orchard

267 views Asked by At

I want to access the selectedvalue (enumeration field) in content part when click submit save button in widget administrator. How I can do that in Editor Method?

1

There are 1 answers

0
Velair On

If you know the name of the ContentPart that has that field, you can do it like this:

(dynamic)contentItem.ContentPartName.FieldName.SelectedValue

But if you don't know the name of the ContentPart, you can first use this to get all the fields of the content item at runtime:

using System.Runtime.CompilerServices;
using Microsoft.CSharp.RuntimeBinder;

// get all the fields from the contentItem without knowing part name

var callSite = CallSite<Func<CallSite, object, object>>
    .Create(Binder.GetMember(0, contentItem.ContentType,
    ((dynamic)contentItem).GetType(), new[] { CSharpArgumentInfo.Create(0, null) }));

var contentItemFields = ((callSite.Target(callSite, ((dynamic)contentItem))).Fields) as List<ContentField>;

Having the list of fields, you now can search for the EnumerationField that you want, and get the selected value:

var yourField = (contentItemFields.FirstOrDefault(f => f.name == "YourField")) as EnumerationField;
var selectedValue = yourField.SelectedValue;