@page "/admin/blogposts/new" @page "/admin/blogposts/{Id}" @inject IBlogApi _api @inject NavigationManager _manager @using Components.RazorComponents @using Markdig; @foreach (var category in Categories) { }
    @foreach (var tag in Tags) {
  • @tag.Name @if (Post.Tags.Any(t => t.Id == tag.Id)) { } else { }
  • }
@((MarkupString)markDownAsHTML) @code{ [Parameter] public string? Id { get; set; } BlogPost Post { get; set; } = new(); List Categories { get; set; } = new(); List Tags { get; set; } = new(); string? selectedCategory = null; string? markDownAsHTML { get; set; } BlogNavigationLock? NavigationLock { get; set; } MarkdownPipeline pipeline = default!; protected override Task OnInitializedAsync() { pipeline = new MarkdownPipelineBuilder() .UseEmojiAndSmiley() .Build(); return base.OnInitializedAsync(); } protected void UpdateHTML() { markDownAsHTML = Markdig.Markdown.ToHtml(Post.Text, pipeline); } bool hasTag(Tag tag) { return Post.Tags.Contains(tag); } protected override async Task OnParametersSetAsync() { if (Id != null) { var p = await _api.GetBlogPostAsync(Id); if (p != null) { Post = p; if (Post.Category != null) { selectedCategory = Post.Category.Id; } UpdateHTML(); } } Categories = (await _api.GetCategoriesAsync())??new(); Tags = (await _api.GetTagsAsync())?? new(); base.OnParametersSet(); } public async Task SavePost() { if (!string.IsNullOrEmpty(selectedCategory) && Categories != null) { var category = Categories.FirstOrDefault(c => c.Id == selectedCategory); if (category != null) { Post.Category = category; } } await _api.SaveBlogPostAsync(Post); NavigationLock?.CurrentEditContext.MarkAsUnmodified(); _manager.NavigateTo("/admin/blogposts"); } }