4

I am converting a DWT into a Razor template. In the Razor template I have a foreach loop. Inside of that foreach loop I am attempting to render a field of the current item using RenderComponentField. In the DWT this was done as follows:

<!-- TemplateBeginRepeat name="MultivaluedField" -->
    @@RenderComponentField(FieldPath+".Target", 0)@@
<!-- TemplateEndIf -->

And so far in Razor I have this:

@foreach (var item in Fields.MultivaluedField) {
    @RenderComponentField(???+".Target", 0)
}

Where "MultivaluedField" is a field which takes takes multiple values of an embedded schema. I cannot figure out how to get the field path of the item in this loop. I've tried item.fieldPath, and item.Path. I've also looked over the razor documentation. Perhaps RenderComponentField is not used in Razor templating, and I am searching for the wrong answer.

How would one access the field path of an item inside of a foreach loop using razor templating?

1 Answer 1

4

I accessed an item using a for loop, and having an index. There might be a better solution though.

@for (int i = 0; i < Fields.MultivaluedField.Count; i++) {
    @RenderComponentField("Fields.MultivaluedField["+i+"].Target", 0)
}
1
  • This answer certainly works. Instead of getting the field path with code, I simply place it in manually. I have the same doubts as you @Sabarish it seems there should be a cleaner solution out there somewhere which uses the foreach loop. Aug 11, 2015 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.