Skip to content

Instantly share code, notes, and snippets.

@Mirch Mirch/Feed.cshtml
Created Jan 12, 2019

Embed
What would you like to do?
@inject HttpClient Http
<div>
@if (posts == null)
{
<p>Loading...</p>
}
else
{
foreach (var post in posts)
{
<Post Title="@post.Title">
@post.Content
</Post>
}
}
</div>
@functions {
[Parameter]
private string Filter { get; set; }
IEnumerable<FeedElement> posts;
protected override async Task OnInitAsync()
{
await Load();
}
protected override async void OnParametersSet()
{
await Load();
this.StateHasChanged();
}
private async Task Load()
{
posts = await GetData();
if (!string.IsNullOrEmpty(Filter))
{
string filter = Filter.ToLower();
posts = posts.Where(p => p.Title.ToLower().Contains(filter) || p.Content.ToLower().Contains(filter));
}
}
private async Task<IEnumerable<FeedElement>> GetData()
{
var data = await Http.GetJsonAsync<IEnumerable<FeedElement>>("sample-data/feed.json");
return data;
}
public class FeedElement
{
public string Title { get; set; }
public string Content { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.