Take the 2-minute tour ×
Tridion Stack Exchange is a question and answer site for Tridion developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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 –  Pankaj Gaur Jul 28 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 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 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. –  Pankaj Gaur Jul 28 at 11:14

1 Answer 1

up vote 5 down vote accepted

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)
    {
        ...
    }
}
share|improve this answer
    
Thanks Ant P. This worked for me. –  guest2014 Aug 1 at 7:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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