0

I'm setting up a simple ASP.NET Core MVC project that includes some partial views that I want to be rendered asynchronously. But it seems that, regardless whether they are rendered asynchronously or not I keep getting the same ExecuteAsync() NullReferenceException error.

I have checked all the models and the HTML code but it doesn't seem to work either way, always returning:

An unhandled exception occurred while processing the request. NullReferenceException: Object reference not set to an instance of an object. AspNetCore.Views_Home_Index.ExecuteAsync() in Index.cshtml, line 4.

Index.cshtml:

@using BestDeal.ViewModels
@model HomeViewModel

@await Html.PartialAsync("ArtikalCarouselPocetna")

<h2>Istaknuti artikli</h2>
<div class="row">
    @foreach (var artikal in Model.odabraniArtikli)
    {
        @Html.Partial("ArtikalPregled", artikal)
    }
</div>

ArtikalCarouselPocetna partial view:


<div class="row carousel-holder marginTop1">
    <div class="col-md-12">
        <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
            <ol class="carousel-indicators">
                <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
                <li data-target="#carousel-example-generic" data-slide-to="1"></li>
                <li data-target="#carousel-example-generic" data-slide-to="2"></li>
            </ol>
            <div class="carousel-inner">
                <div class="item active">
                    <img class="slide-image" src="https://images-na.ssl-images-amazon.com/images/I/71t-J3VJtEL._SX425_.jpg" alt="">
                </div>
                <div class="item">
                    <img class="slide-image" src="https://zdnet1.cbsistatic.com/hub/i/r/2019/04/17/1f68c3a6-495e-4325-bc16-cc531812f0ec/thumbnail/770x433/84ff4194826e8303efb771cd377a854f/chuwi-herobook-header.jpg" alt="">
                </div>
                <div class="item">
                    <img class="slide-image" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRKRl7BqXfZupIBH4N8i-tD45gVPctFV5jKTeTmOIADFhZ8J_DAYQ" alt="">
                </div>
            </div>
            <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left"></span>
            </a>
            <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right"></span>
            </a>
        </div>
    </div>
</div>

My Views folder

123rightback123
  • 114
  • 2
  • 2
  • 13

1 Answers1

1

Your error in, I think you model is null. So you can check it like this if(model != null) then

@foreach (var artikal in Model.odabraniArtikli)
    {
        @Html.Partial("ArtikalPregled", artikal)
    }

So, I see you view located in different folder, it's mean you need point full path

@await Html.PartialAsync("~/Views/Shered/ArtikalCarouselPocetna.cshtml")

but I suggest you better use

@{
    await Html.RenderPartialAsync("~/Views/Shered/ArtikalCarouselPocetna.cshtml");
}

Alternatively, you can render a partial view with RenderPartialAsync. This method doesn't return an IHtmlContent. It streams the rendered output directly to the response. Because the method doesn't return a result, it must be called within a Razor code block:

@{
    await Html.RenderPartialAsync("_AuthorPartial");
}

Since RenderPartialAsync streams rendered content, it provides better performance in some scenarios. In performance-critical situations, benchmark the page using both approaches and use the approach that generates a faster response.

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-2.2

Also I advise you use partial view with _ https://stackoverflow.com/a/10321458/8006943

evilGenius
  • 1,041
  • 1
  • 7
  • 16
  • I'm afraid neither one of the suggestions works. It's now highlighting **RenderPartialAsync**, but the error stays the same: _AspNetCore.Views_Home_Index.ExecuteAsync() in Index.cshtml, line 5_ – 123rightback123 Jun 09 '19 at 12:38
  • it's look weird, in your partial view you not use any model. Could you comment your loop with model and try again.(@foreach (var artikal in Model.odabraniArtikli) { @Html.Partial("ArtikalPregled", artikal) }) – evilGenius Jun 09 '19 at 12:55
  • It works pretty fine without it, but it doesn't load the HTML (I'm surprised to an extent). What could be causing such a behaviour? – 123rightback123 Jun 09 '19 at 13:16
  • Your Model can be null or your odabraniArtikli can be null, or you partial can be getting null. So I will update my answer, cuz I add some good advise. – evilGenius Jun 09 '19 at 13:19
  • So man I need go, you need just understand where you have null in you model, just you breakpoint for figured out it – evilGenius Jun 09 '19 at 13:24
  • Your suggestions eventually lead me to the source of the problem, I sent a wrong container deep inside the interface and viewmodel hierarchy. Thank you @evilGenius ! – 123rightback123 Jun 09 '19 at 16:45