4

I have the following syntax in a template using Razor Mediator:

<tcdl:ComponentField name="Fields.contact_nav">
    @foreach (dynamic link in Fields.contact_nav.links) {
        @if(link.IsFirst){...}
    }
</tcdl:ComponentField>

How do I write the link.IsFirst in MVC Razor? I have done the following:

@Html.SiteEditField(Model.Component,Model.Component.Fields["contact_nav"])
@foreach (var link in Model.Component.Fields["contact_nav"].LinkedComponentValues) {
    if(link.EmbeddedValues.First()){...}
}

But it is giving error for EmbeddedValues.

What am I doing wrong? How do I check whether a field is the first in a list or not in MVC Razor?

4
  • The main thing that you are doing wrong is asking MVC Razor question in the Tridion community instead of .NET/MVC specific community :) I hope in communities like Stackoverflow you will get more responses which may help resolving your question quickly and effectively. Please note, Razor mediator is a wrapper over MVC Razor and they may have different syntex Jul 28, 2014 at 9:49
  • @PankajGaur I don't think this is an unreasonable question for this site. It is primarily programming related, yes, but it is heavily focused on the context of Tridion - just because it's on-topic elsewhere doesn't mean it's off-topic here.
    – Ant P
    Jul 28, 2014 at 9:52
  • 2
    Specifically, it's entirely reasonable to expect that this problem might be solved by some part of the DD4T API, which is explicitly on-topic as defined by "programming related to Tridion and Tridion extensions."
    – Ant P
    Jul 28, 2014 at 10:00
  • If community think so...I have no issues, however, as far as I know many such question which are in context with SDL Tridion but is related to some other technology or is related to programming have been closed in past. Jul 28, 2014 at 11:14

1 Answer 1

5

To expand a little on what your problem is and why your solution won't work; the IsFirst property that you're using is part of the Razor Mediator's DynamicItemFields class and is explicitly set by the library, not a built-in part of any .NET collection type.

By contrast, when you use the Enumerable.First method as in your MVC example, you are not checking if the link is the first in the list, you are querying for the first embedded value in link that matches the provided expression (or in this case, as you pass no expression, the first embedded value in the link field), although since link is probably not an embeddable schema, there likely won't actually be any embedded values (not that it would help you if there were).

The most robust solution is probably just to set a flag:

@{
    bool first = true;
}
@foreach(var link in Model.Component.Fields["contact_nav"].LinkedComponentValues)
{
    if (first)
    {
        first = false;
        ...
    }
}

If you need more sophisticated index-related logic you would probably be better off using a standard for loop rather than a foreach.

@{
    var values = Model.Component.Fields["contact_nav"].LinkedComponentValues;
}

@for(int i = 0; i < values.Count; i++)
{
    if(i == 0)
    {
        ...
    }
}
0

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.