Created
February 13, 2020 23:32
-
-
Save csharpfritz/c4dcfcc731826822e0764728bbd9d88c to your computer and use it in GitHub Desktop.
Complex object binding to a Blazor component as a Parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h3>Foo</h3> | |
@if (NavItems != null && NavItems.Count() > 0) | |
{ | |
<ul> | |
@foreach (var item in NavItems) | |
{ | |
<li>@item.Title</li> | |
} | |
</ul> | |
} else | |
{ | |
<span>Loading...</span> | |
} | |
@code { | |
[Parameter] | |
public IEnumerable<BlazorApp2.Data.Nav> NavItems { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page "/" | |
@using System.Text.Json | |
<h1>Hello, world!</h1> | |
Welcome to your new app. | |
<Foo NavItems="TheItems"></Foo> | |
@code { | |
public List<BlazorApp2.Data.Nav> TheItems { get; set; } | |
private string theJson = "..."; | |
protected override Task OnInitializedAsync() | |
{ | |
var theNavs = JsonSerializer.Deserialize<BlazorApp2.Data.Nav[]>(theJson, new JsonSerializerOptions | |
{ | |
PropertyNameCaseInsensitive=true | |
}); | |
TheItems = theNavs; | |
return base.OnInitializedAsync(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment