Home > front end >  I have problem with partial view in mvc core 3.1
I have problem with partial view in mvc core 3.1

Time:06-15

I get an error in

foreach (var item in *Model*)

Model is Null - using ASP.NET Core 3.1

NullReferenceException: Object reference not set to an instance of an object.

AspNetCore.Views_Products_MyIndex.ExecuteAsync() in MyIndex.cshtml

@foreach (var item in Model)

Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.RenderPartialCoreAsync(string partialViewName, object model, ViewDataDictionary viewData, TextWriter writer)
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.PartialAsync(string partialViewName, object model, ViewDataDictionary viewData) AspNetCore.Views_Shared__Layout_Pigall.b__20_1() in _Layout_Pigall.cshtml

@await Html.PartialAsync("/Views/Products/MyIndex.cshtml")

Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext .SetOutputContentAsync()
AspNetCore.Views_Shared__Layout_Pigall.ExecuteAsync()

Partial view

@model IEnumerable<DataAccessLayer.ViewModels.ProductViewModel>
<section >
    <div >
        <div >
            <div >
                    @foreach (var item in Model)
                    {
                    <a  href="">

                        <img src="images/@item.ImageName" alt="" >
                        <img src="images/@item.ImageName" alt=""
                        >
                    </a>

                    <a  href="">
                        <img src="images/slide2.jpg" alt="" >
                        <img src="images/slider-mob2.jpg" alt=""
                        >
                    </a>
                    }

            </div>
        </div>
    </div>
</section>

Service

async Task<IEnumerable<ProductViewModel>> IProductRepositories.GetDataForProductViewModels()
{
    return await db.Products
                   .Select(x => new ProductViewModel()
                                {
                                    ID = x.ID,
                                    Title = x.Title,
                                    ShortDescription = x.ShortDescription,
                                    ImageName = x.ImageName,
                                    Count = x.Count,
                                    Fabric = x.Fabric,
                                    Price = x.Price,
                                    ShowInSlider = x.ShowInSlider,
                                    PageCount = 0
                                })
                   .ToListAsync();
}    

Layout

@await Html.PartialAsync("/Views/Products/MyIndex.cshtml")

CodePudding user response:

The following line of code, you can see through the code, it will not enter the breakpoint in the Products/MyIndex method. So it will not read the data in your IProductRepositories.GetDataForProductViewModels.

@await Html.PartialAsync("/Views/Products/MyIndex.cshtml")

@await Html.PartialAsync is generally no problem for loading static resources.If you need to load data, you can add the following code to the page you need to open.

Sample Code

Home/Index

public IActionResult Index()
    {
        List<ProductViewModel> list = new List<ProductViewModel>();
        ProductViewModel p = new ProductViewModel();
        for (int i = 1; i < 11; i  )
        {
            p.ID = i;
            p.Title = "Title"   i;
            p.ImageName = "ImageName"   i;
            list.Add(p);
        }

        ViewBag.aa = list;

        return View();
    }

_Layout

@await Html.PartialAsync("/Views/Products/MyIndex.cshtml", (List<ProductViewModel>)ViewBag.aa)

Suggestion

It is not recommended to use Html.PartialAsync on Layout pages.

  • Related