BlowFish
BlowFish

Reputation: 183

Sort by Value in Razor using Javascript

 <ul class="related_forms_list">
    @foreach (var year in test.years ?? new List<YearsVM>())
        {
            <li><a href="@Url.Action("Test","Link", new { id=Year.Id })">@Year.Year</a></li>
        }

My code above pulls a list of years that is gathered from my database. However, they aren't organized by year. I was thinking Javascript would be the best way to do this but I am unsure of how to write the javascript so it affects my list.

Upvotes: 2

Views: 164

Answers (1)

haim770
haim770

Reputation: 49105

You can simply OrderBy() in Razor:

@foreach (var year in (test.years ?? new List<YearsVM>()).OrderBy(x => x.Year))

(By encapsulating (test.years ?? new List<YearsVM>()) in its own brackets)

See MSDN

Upvotes: 1

Related Questions