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
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)
C#-Quellcode
- @page "/"
- <h4>Cascading Cmbx NAMES vs NUMMBERS</h4>
- <hr />
- <h4>First</h4>
- <select class="form-control" @bind="FirstCmbxSelectedGuid">
- <option value=@FirstCmbxSelectedGuid selected disabled="disabled">--Select--</option>
- @foreach (var item in FirstCmbxItemLst)
- {
- <option value="@item.Id">@item.MyString</option>
- }
- </select>
- @*<p>sel. Obj.-Guid:@FirstCmbxSelectedItemObj.Id ---- sel. Obj.-String:@FirstCmbxSelectedItemObj.MyString</p>*@
- <br />
- <h4>Second</h4>
- <select class="form-control" @bind="SecondCmbxSelecetedGuid">
- <option value=@(SecondCmbxSelecetedGuid) selected disabled="disabled">--Select--</option>
- @if (MySecondCmbxActivLst.Count > 0)
- {
- @foreach (var item in MySecondCmbxActivLst)
- {
- <option value="@item.Id">@item.MyString</option>
- }
- }
- </select>
- @*<p>sel. Obj.-Guid:@SecondCmbxSelectedItemObj?.Id ---- sel. Obj.-String:@SecondCmbxSelectedItemObj?.MyString</p>*@
- <hr />
- <h4>@FirstCmbxSelectedItemObj.MyString @SecondCmbxSelectedItemObj?.MyString</h4>
- @code{
- //---DataModel--
- public class MyGuidStringItem
- {
- public Guid Id { get; set; }
- public string MyString { get; set; }
- //Consturctors
- public MyGuidStringItem()
- { }
- public MyGuidStringItem(string myString)
- {
- Id = Guid.NewGuid();
- MyString = myString;
- }
- }
- public List<MyGuidStringItem> FirstCmbxItemLst = new()
- {
- new MyGuidStringItem("Names"),
- new MyGuidStringItem("Nummbers")
- };
- public List<MyGuidStringItem> SecondCmbxItemNameLst = new()
- {
- new MyGuidStringItem("Alfred"),
- new MyGuidStringItem("Jonny"),
- new MyGuidStringItem("July")
- };
- public List<MyGuidStringItem> SecondCmbxItemIntLst = new()
- {
- new MyGuidStringItem("123"),
- new MyGuidStringItem("456"),
- new MyGuidStringItem("678")
- };
- //^^^DataModel--
- //wegen <option value=@FirstCmbxSelectedGuid) selected disabled="disabled">--Select--</option>
- //Default-Werte @startup ... .FirstOrDefault();
- protected override void OnInitialized()
- {
- FirstCmbxSelectedGuid = default;
- FirstCmbxSelectedItemObj = new();
- }
- public List<MyGuidStringItem> MySecondCmbxActivLst = new();
- public MyGuidStringItem FirstCmbxSelectedItemObj;
- private Guid _firstCmbxSelectedGuid;
- public Guid FirstCmbxSelectedGuid
- {
- get => _firstCmbxSelectedGuid;
- set
- {
- _firstCmbxSelectedGuid = value;
- SecondCmbxSelecetedGuid = default;
- FirstCmbxSelectedItemObj = FirstCmbxItemLst.Where<MyGuidStringItem>(x => x.Id == FirstCmbxSelectedGuid).FirstOrDefault();
- //Achtung ist noch sowas wie 'MagicString'(feste ListPosition) geht nicht ganz ohne
- if (_firstCmbxSelectedGuid == FirstCmbxItemLst[0].Id)
- {
- MySecondCmbxActivLst = SecondCmbxItemNameLst;
- }
- if (_firstCmbxSelectedGuid == FirstCmbxItemLst[1].Id)
- {
- MySecondCmbxActivLst = SecondCmbxItemIntLst;
- }
- }
- }
- public MyGuidStringItem SecondCmbxSelectedItemObj = new();
- private Guid _secondCmbxSelecetedGuid;
- public Guid SecondCmbxSelecetedGuid
- {
- get => _secondCmbxSelecetedGuid;
- set
- {
- _secondCmbxSelecetedGuid = value;
- SecondCmbxSelectedItemObj = MySecondCmbxActivLst.Where<MyGuidStringItem>(x => x.Id == _secondCmbxSelecetedGuid).FirstOrDefault();
- }
- }
- }
codewars.com Rank: 4 kyu