Skip to content

Instantly share code, notes, and snippets.

@csharpfritz
Created February 13, 2020 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csharpfritz/c4dcfcc731826822e0764728bbd9d88c to your computer and use it in GitHub Desktop.
Save csharpfritz/c4dcfcc731826822e0764728bbd9d88c to your computer and use it in GitHub Desktop.
Complex object binding to a Blazor component as a Parameter
<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; }
}
@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();
}
}
public class Nav
{
public string Title { get; set; }
public string Icon { get; set; }
public string Url { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment