5

Is there any better, cleaner, way to set selected="true" ? Maybe a nested if?

<select id="State" name="State" if="(Model.StateList.Count() > 0 )" >
    <option value="">Select One</option>
    <for each="KeyValuePair<string, string> item in Model.StateList">
        <option value="${item.Value}" if="(Model.State == item.Value)" selected="true" >${item.Key}</option>
        <option value="${item.Value}" if="(Model.State != item.Value)">${item.Key}</option>
      </for>
  </select>
detroitpro
  • 3,853
  • 4
  • 35
  • 64

1 Answers1

8
<select id="State" name="State" if="Model.StateList.Any()" >
        <option value="">Select One</option>
        <option each="var item in Model.StateList" value="${item.Value}" selected="true?{Model.State == item.Value}" >${item.Key}</option>
</select>
loudej
  • 1,889
  • 11
  • 17
  • By the way I only found this feature recently in some PPT presentation; I couldn't find it on the Spark web site. – queen3 Dec 09 '09 at 12:28
  • Now it's there (http://sparkviewengine.com/documentation/expressions) under `Conditional attribute output`. Don't know about Dec 9. – Arnis Lapsa Jan 16 '10 at 21:21
  • @ArnisLapsa That site seems dead, is there any other source for conditional attributes? – GettnDer Mar 24 '17 at 16:10