1

I am trying to Invoke Components from inside the view, but keep getting errors. And online I can only find examples working with ASP.NET Core, but I am not using Core.

This is my View:

@model BeestjeOpJeFeestje.Models.BoekingProces

<h2>Boeken</h2>
<hr />
<div class="row">
    <div class="col col-9" style="padding: 0">
        @switch (Model.Boeking.BoekingStatus)
        {
            case BeestjeOpJeFeestje.Models.BoekingStatus.Beestjes:
                if (TempData["error"] != null)
                {
                    <span class="text-danger">@TempData["error"]</span>
                }
                @await Component.InvokeAsync("BeestjesKiezen", @Model);
                break;
            case BeestjeOpJeFeestje.Models.BoekingStatus.Accessoires:
                @await Component.InvokeAsync("AccessoiresKiezen", @Model);
                break;
            case BeestjeOpJeFeestje.Models.BoekingStatus.Informatie:
                @await Component.InvokeAsync("Klantgegevens", @Model);
                break;
            case BeestjeOpJeFeestje.Models.BoekingStatus.Bevestiging:
                @await Component.InvokeAsync("Bevestiging", @Model);
                break;
        }
    </div>
    <div class="col col-3">
        @await Component.InvokeAsync("BookingDetail", @Model)
    </div>
</div>

But it results in the errors that 'await' and 'Component' not exists in the current context. enter image description here

Can someone maybe help me with the correct syntax? Thanks in advance.

Marcel
  • 398
  • 2
  • 4
  • 22
  • Did you try await without @ inside switch? – Adlorem Mar 11 '20 at 18:50
  • When I Try it without the @ The Component error stays the same. And the await errors becomes: 'The 'await operator can only be used within an async method...'' – Marcel Mar 11 '20 at 18:55
  • Are you referencing static class Component with using, because I don't see it in your code. – Adlorem Mar 11 '20 at 20:15
  • No I do not. But I am not really sure why this isn't working. I have this exact code in a Core project and there it is working fine. Could it be that it just is not possible in ASP.NET MVC? – Marcel Mar 11 '20 at 20:59

1 Answers1

1
  1. This question has been answered before, the Component Static class is not available in ASP.NET MVC. It is only available with Core; Asp.net mvc compiler error when trying to invoke View Component from view page

  1. It is mentioned in the documentation that it is similar to a partial view, hence use that instead. But this won't have the asynchronous feature, the server will always wait for this rendering to finish before proceeding.
@{
    Html.RenderPartial("BeestjesKiezen", Model);
}

  1. Meanwhile, rendering parts of your page asynchronously can be done via ajax/jquery. See the answer here; How to render a partial view asynchronously
Jerdine Sabio
  • 5,688
  • 2
  • 11
  • 23
  • 1
    I couldn't figure the async loading out, so i went ahead an made everything synchronous. It all works fine now. – Marcel Mar 12 '20 at 11:11