Search code examples
asp.net-mvcrazorrazor-pages

My partial views are not rendered by the browser


I am trying to make a simple razor pages project in .net 6.

I have this partial view:

@model TaskManagement.TaskModel
<div class = "Task">
    <h3>@Model.Title</h3>
    <p>@Model.Category</p>
    <p>@Model.Description</p>
    <p>a task</p>
    <p>@Model.DueDate.ToShortDateString()</p>
</div>

and I reference it in this razor page:

@page 
@model TaskManagement.IndexModel
@{
    Layout = "shared/_MainLayout";
    ViewData["Title"] = "Home";
}
<ul class = "Tasks">
    <h3>Title</h3>
    <h3>Category</h3>
    <h3>Description</h3>
    <h3>Due Date</h3>
    @foreach (var task in Model.Tasks)
    {
        <p>a task </p>
        <partial name="/Pages/_Task" model= "task" />
    }
</ul>

which is connected to this model :

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.VisualBasic;

namespace TaskManagement
{
    public class IndexModel : PageModel
    {
        public List<TaskModel> Tasks { get; set; } 
        public void OnGet()
        {
            Tasks = new()
            {
                new()
                {
                    Title = "Code",
                    Description = "Do it in razor pages",
                    Category = "Programming",
                    DueDate = DateTime.Now.AddDays(2)
                },
                new()
                {
                    Title = "Study",
                    Description = "Logical circuits",
                    Category = "KNTU",
                    DueDate = DateTime.Now.AddDays(1)
                }
            };
        }
    }
}

when I run the app and navigate to the Index page, My tasks do not show up, and I get this in my browser( I need the info about each task to be shown bellow "a task", but it's not ):



    Home
    Category

    Title
    Category
    Description
    Due Date

    a task

    a task

Made with love

CopyRight 2023

I must say that I'm 100% sure about the path to the partial page.


Solution

  • When you reference a partial, you can either do so by name if it is located in one of the default search locations, or by its path. If you provide a path, you have to include the file extension:

    <partial name="/Pages/_Task.cshtml" model="task" />
    

    Given that the Pages folder is one of the default search locations for partials, you can just provide the name without the extension:

    <partial name="_Task" model="task" />