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.

4
  • Did you try await without @ inside switch? Commented Mar 11, 2020 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...'' Commented Mar 11, 2020 at 18:55
  • Are you referencing static class Component with using, because I don't see it in your code. Commented Mar 11, 2020 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? Commented Mar 11, 2020 at 20:59

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

I couldn't figure the async loading out, so i went ahead an made everything synchronous. It all works fine now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.