Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jul 20, 2011 @ 09:29
    Bo Damgaard Mortensen
    0

    Weird dropdown behavior: displays the wrong value

    Hi all,

    I'm trying to populate an HTML dropdown (select, rather) with all prevalues from the dropdown datatype in Umbraco and then set the selected prevalue when the document loads.

    I've got it working, but the dropdown selects the wrong value even though the right option tag is set to selected.

    The options gets populated like this in HTML:

    <select data-bind="selectedOptions: selectedLength">
     <option value='1 time'>1 time</option>
      <option value='1 - 2 timer'>1 - 2 timer</option>
      <option value='1 - 3 timer'>1 - 3 timer</option>
      <option value='1 - 4 timer'>1 - 4 timer</option>
      <option value='2 timer'>2 timer</option>
      <option value='2 - 3 timer'>2 - 3 timer</option>
      <option value='2 - 4 timer'>2 - 4 timer</option>
      <option value='2 - 6 timer'>2 - 6 timer</option>
      <option value='3 timer'>3 timer</option>
      <option value='3 - 4 timer'>3 - 4 timer</option>
      <option value='3 - 5 timer'>3 - 5 timer</option>
      <option class="initial" selected="selected" value='3 - 6 timer'>3 - 6 timer</option>
      <option value='4 timer'>4 timer</option>
      <option value='4 - 5 timer'>4 - 5 timer</option>
      <option value='4 - 6 timer'>4 - 6 timer</option>
    .....
    <option value="6 timer">6 timer</option>

    As you can see, the value of the selected option is "3 - 6 timer", but on the page it displays the "6 timer" option as selected?

    My Razor looks like this:

    <select data-bind="selectedOptions: selectedLength">
                @{
               // Get prevalues from the length dropdown datatype
               XPathNodeIterator lengthPreValueRootElementIterator umbraco.library.GetPreValues(1102);
               lengthPreValueRootElementIterator.MoveNext()//move to first
               preValueIterator lengthPreValueRootElementIterator.Current.SelectChildren("preValue""");     

               List<stringumbPreValues new List<string>();           
               
               while(preValueIterator.MoveNext())
               {
                  umbPreValues.Add(preValueIterator.Current.Value);              
               }
                  
               foreach(string preValue in umbPreValues)
               {
                  if(preValue.Equals(node.varighed))
                  {
                    <option class="initial" selected="selected" value='@node.varighed'>@node.varighed</option>
                  }     
                  else
                  {
                    <option value='@preValue'>@preValue</option>
                  }             
               }
             }
             </select>

    I must admit that I fail to see the flaw in this - simply because the selected value is set to the right prevalue in the HTML source.

    And it's not because the "6 timer" is the last prevalue in the list. It goes up to "10 timer" (timer = hours in danish)

    Any help on this would be greatly appreciated! :)

    Thanks in advance.

    All the best,

    Bo

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jul 20, 2011 @ 09:34
    Bo Damgaard Mortensen
    0

    The only thing I can come up with in this, is that "6 timer" is a part of the "3 - 6 timer" string, but it still doesn't make any sense to me. If I choose e.g. 3 - 4 timer in the backend, it will display "4 timer" in the dropdown menu on the frontend, even though the selected option says value="3 - 4 timer"

    I don't get it ;-)

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jul 21, 2011 @ 08:09
    Bo Damgaard Mortensen
    0

    Alright, with strange issues comes strange fixes - all of sudden it started to work today ;-) Wish I could explain what I did to make it work.

Please Sign in or register to post replies

Write your reply to:

Draft