Blazor 'Casading ComboBoxen' Beispiel mit Platzhalter

    • (Core) Blazor WebAssembly
    • C# (ASP)

      Blazor 'Casading ComboBoxen' Beispiel mit Platzhalter

      Hi,

      ich hab ein paar Tage gebraucht, um mir geschachtelte Comboboxen in Blazor zu erarbeiten (da ich relativ neu im Blazor/Razor/Asp.net/-Business bin ohne gewähr).
      Vielleichte hilft es dem ein oder anderen etwas schneller ans Ziel zu kommen :)

      Der Code kann Kopie/Paste in das Blazor Template eingefügt werden (komplett ohne Fremdlibrarys Telerik, Syncfusion,...).
      Für mich war es nicht trivial es korrekt mit Platzhaltern hinzubekommen.

      Zum schnellen Verstehen .gif im Anhang anklicken.



      (Index.razor mir dem Code ersetzen)
      Spoiler anzeigen

      C#-Quellcode

      1. @page "/"
      2. <h4>Cascading Cmbx NAMES vs NUMMBERS</h4>
      3. <hr />
      4. <h4>First</h4>
      5. <select class="form-control" @bind="FirstCmbxSelectedGuid">
      6. <option value=@FirstCmbxSelectedGuid selected disabled="disabled">--Select--</option>
      7. @foreach (var item in FirstCmbxItemLst)
      8. {
      9. <option value="@item.Id">@item.MyString</option>
      10. }
      11. </select>
      12. @*<p>sel. Obj.-Guid:@FirstCmbxSelectedItemObj.Id ---- sel. Obj.-String:@FirstCmbxSelectedItemObj.MyString</p>*@
      13. <br />
      14. <h4>Second</h4>
      15. <select class="form-control" @bind="SecondCmbxSelecetedGuid">
      16. <option value=@(SecondCmbxSelecetedGuid) selected disabled="disabled">--Select--</option>
      17. @if (MySecondCmbxActivLst.Count > 0)
      18. {
      19. @foreach (var item in MySecondCmbxActivLst)
      20. {
      21. <option value="@item.Id">@item.MyString</option>
      22. }
      23. }
      24. </select>
      25. @*<p>sel. Obj.-Guid:@SecondCmbxSelectedItemObj?.Id ---- sel. Obj.-String:@SecondCmbxSelectedItemObj?.MyString</p>*@
      26. <hr />
      27. <h4>@FirstCmbxSelectedItemObj.MyString @SecondCmbxSelectedItemObj?.MyString</h4>
      28. @code{
      29. //---DataModel--
      30. public class MyGuidStringItem
      31. {
      32. public Guid Id { get; set; }
      33. public string MyString { get; set; }
      34. //Consturctors
      35. public MyGuidStringItem()
      36. { }
      37. public MyGuidStringItem(string myString)
      38. {
      39. Id = Guid.NewGuid();
      40. MyString = myString;
      41. }
      42. }
      43. public List<MyGuidStringItem> FirstCmbxItemLst = new()
      44. {
      45. new MyGuidStringItem("Names"),
      46. new MyGuidStringItem("Nummbers")
      47. };
      48. public List<MyGuidStringItem> SecondCmbxItemNameLst = new()
      49. {
      50. new MyGuidStringItem("Alfred"),
      51. new MyGuidStringItem("Jonny"),
      52. new MyGuidStringItem("July")
      53. };
      54. public List<MyGuidStringItem> SecondCmbxItemIntLst = new()
      55. {
      56. new MyGuidStringItem("123"),
      57. new MyGuidStringItem("456"),
      58. new MyGuidStringItem("678")
      59. };
      60. //^^^DataModel--
      61. //wegen <option value=@FirstCmbxSelectedGuid) selected disabled="disabled">--Select--</option>
      62. //Default-Werte @startup ... .FirstOrDefault();
      63. protected override void OnInitialized()
      64. {
      65. FirstCmbxSelectedGuid = default;
      66. FirstCmbxSelectedItemObj = new();
      67. }
      68. public List<MyGuidStringItem> MySecondCmbxActivLst = new();
      69. public MyGuidStringItem FirstCmbxSelectedItemObj;
      70. private Guid _firstCmbxSelectedGuid;
      71. public Guid FirstCmbxSelectedGuid
      72. {
      73. get => _firstCmbxSelectedGuid;
      74. set
      75. {
      76. _firstCmbxSelectedGuid = value;
      77. SecondCmbxSelecetedGuid = default;
      78. FirstCmbxSelectedItemObj = FirstCmbxItemLst.Where<MyGuidStringItem>(x => x.Id == FirstCmbxSelectedGuid).FirstOrDefault();
      79. //Achtung ist noch sowas wie 'MagicString'(feste ListPosition) geht nicht ganz ohne
      80. if (_firstCmbxSelectedGuid == FirstCmbxItemLst[0].Id)
      81. {
      82. MySecondCmbxActivLst = SecondCmbxItemNameLst;
      83. }
      84. if (_firstCmbxSelectedGuid == FirstCmbxItemLst[1].Id)
      85. {
      86. MySecondCmbxActivLst = SecondCmbxItemIntLst;
      87. }
      88. }
      89. }
      90. public MyGuidStringItem SecondCmbxSelectedItemObj = new();
      91. private Guid _secondCmbxSelecetedGuid;
      92. public Guid SecondCmbxSelecetedGuid
      93. {
      94. get => _secondCmbxSelecetedGuid;
      95. set
      96. {
      97. _secondCmbxSelecetedGuid = value;
      98. SecondCmbxSelectedItemObj = MySecondCmbxActivLst.Where<MyGuidStringItem>(x => x.Id == _secondCmbxSelecetedGuid).FirstOrDefault();
      99. }
      100. }
      101. }
      Bilder
      • CascadingCmbx_byNogood.gif

        328,78 kB, 946×408, 85 mal angesehen
      codewars.com Rank: 4 kyu