Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Vlad 4 posts 94 karma points
    Jan 11, 2016 @ 02:12
    Vlad
    0

    Sort content in partial view

    Hi

    I have the following partial view:

    var cts = ApplicationContext.Current.Services.ContentTypeService;
    var masterDocTypeId = cts.GetContentType("Brand").Id;
    
    var cs = ApplicationContext.Current.Services.ContentService;
    var brands = cs.GetContentOfContentType(masterDocTypeId);
    

    ...

                        <p class="text-center">
                        @foreach (var brand in brands)
                        {
                            dynamic b = Umbraco.Content(brand.Id);
                            <p>@b.Name</p>
                        }
                        </p>
    

    How can I sort brands by a certain property?

    Thanks V.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 11, 2016 @ 11:54
    Alex Skrypnyk
    100

    Hi Vlad,

    GetContentOfContentType returns IEnumerable of Content items, you don't need 'dynamic b = Umbraco.Content(brand.Id);'

    Try to sort like :

    var brands = ApplicationContext.Services.ContentService.GetContentOfContentType(contentTypeId).OrderBy(x => x.GetValue<T>("propertyAlias"));
    

    Thanks,

    Alex

  • Vlad 4 posts 94 karma points
    Jan 12, 2016 @ 02:49
    Vlad
    0

    Thanks Alex

  • Charles Afford 1163 posts 1709 karma points
    Jan 11, 2016 @ 13:04
    Charles Afford
    0

    Vlad I would have a look at Object Relational Mappers like Glass It would stop you having to put magic strings in like 'Brand'. Also i would abstract this is to a helper class and call it. Also i would put some exception handling in

    But as Alex says for sure. Just do an OrderBy()

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 12, 2016 @ 10:28
    Alex Skrypnyk
    0

    Thanks, Charles, great suggestions. I think if we need just sort items on presentation layer, OrderBy is enough.

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft