Copied to clipboard

Flag this post as spam?

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


  • Robbie van den Hoven 36 posts 181 karma points
    Oct 22, 2012 @ 09:59
    Robbie van den Hoven
    0

    How to display the items by sortId in Razor?

    I have a list of items displayed at 'creation' id likte this:

     dynamic brochureLijst = Model.brochures.Item;
         if (brochureLijst.Count() != 0)
            {
                <ul>
                @foreach (var brochure in brochureLijst)
                {
                 <li> @brochure.naam</li>
                }
                </ul>
            }

    But I would like to display them by there 'sortId'

    I can't get this work. The 'soloutions' I found are based on nodes. How do I sort/order a list of items?

     <brochures>
            <Items>
              <Item id="1" sortId="2">
                <naam><![CDATA[Peter]]></naam>
              </Item>
              <Item id="2" sortId="3">
                <naam><![CDATA[Jack]]></naam>
              </Item>
              <Item id="3" sortId="1">
                <naam><![CDATA[John]]></naam>
              </Item>
            </Items>
          </brochures>

     

  • aghy 129 posts 308 karma points
    Oct 24, 2012 @ 12:50
    aghy
    0

    Hi Robbie,

    I think you should be able to do something like:

    @foreach(var brochure in brochureLijst.OrderBy(item => item.sortId))

    This is just a guess though so let me know how you get on.

    Thanks
    Ben

  • Robbie van den Hoven 36 posts 181 karma points
    Oct 24, 2012 @ 14:34
    Robbie van den Hoven
    0

    Hi Ben,

    I also thought that it wil do the trick.

    So I tried it again, but get the error in VS on the lambda saying "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type".
    Is it because of the use of 'dynamic'?

  • aghy 129 posts 308 karma points
    Oct 24, 2012 @ 15:24
    aghy
    0

    Ok, not ideal but the problem is because dynamic isn't IEnumerable, try something like: 

    @{
      List<dynamicitems new List<dynamic>();
        
      foreach (dynamic item in Model.multiType)
      {       
       
       items.Add(new {
         Id item.id,
         sortId item.sortId,
         Name item.name
       });
      }

    }

    @foreach(var item in items.OrderBy(=i.sortId)){
      <p>@item.sortId@item.Name</p>
    }

    Basically you have to create a list of all your items which you can then use lambda expressions on.

    Ben

  • Jesper Ordrup 1019 posts 1528 karma points MVP
    Oct 24, 2012 @ 15:33
    Jesper Ordrup
    0

    Had the same problem. Solved it by abandoning razor. No time for that. Sadly - thats why xslt still will be around for a long time to come :-)

Please Sign in or register to post replies

Write your reply to:

Draft