Displaying all records of a SQL Server table in a Razor Page

hope this helps :stuck_out_tongue_winking_eye:

@page  
@model YourNamespace.IndexModel  
@{  
    ViewData["Title"] = "Index";  
}  

<h1>Records</h1>  

<p>  
    <a asp-page="Create">Create New</a>  
</p>  
<table class="table">  
    <thead>  
        <tr>  
            @* Table headers will be generated based on your model *@  
            <th></th>  
        </tr>  
    </thead>  
    <tbody>  
        @foreach (var item in Model.YourModelName)  
        {  
            <tr>  
                @* Table rows will be generated based on your model *@  
                <td>  
                    <a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |  
                    <a asp-page="./Details" asp-route-id="@item.Id">Details</a> |  
                    <a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>  
                </td>  
            </tr>  
        }  
    </tbody>  
</table>
public class IndexModel : PageModel  
{  
    private readonly YourDbContext _context;  

    public IndexModel(YourDbContext context)  
    {  
        _context = context;  
    }  

    public IList<YourModel> YourModelName { get; set; }  

    public async Task OnGetAsync()  
    {  
        YourModelName = await _context.YourModelName.ToListAsync();  
    }  
}

Troubleshooting Steps:

  1. Check your database to ensure there are records to display
  2. Verify the connection string in appsettings.json is correct
  3. Ensure your model matches the database table structure
  4. Check for any exceptions in the Output window while debugging