Razor Pages中无法在index.cshtml访问index.cshtml.cs创建的列表
问题排查与解决方案
你遇到的核心问题是Razor页面与后台PageModel的匹配错误,同时存在代码冗余和HTML结构缺失,以下是具体修复步骤:
1. 修复Model命名空间不匹配问题
你的index.cshtml.cs文件命名空间是Proiectss.Pages,但前端index.cshtml中引用的Model是Proiectss.Pages.Clients.IndexModel,两者完全不匹配,导致无法识别listClients字段。
- 如果页面位于项目根目录的
Pages文件夹下:
修改index.cshtml的Model引用:@model Proiectss.Pages.IndexModel - 如果页面位于
Pages/Clients子文件夹下:
修改index.cshtml.cs的命名空间为:namespace Proiectss.Pages.Clients
2. 删除构造函数中的错误代码
构造函数里的ViewData["listClients"] = HttpContext.Features.Get<Proiectss.Pages.Clients.IndexModel>().listClients;完全无效,HttpContext.Features不会存储你的PageModel实例,这行代码不仅没用,还会触发空引用异常,直接删除即可。
3. 规范字段为属性(可选但推荐)
虽然Razor可以访问公共字段,但ASP.NET官方推荐使用属性来暴露数据,将后台的字段改为属性:
// 原代码 public List<ClientInfo> listClients = new List<ClientInfo>(); // 修改后 public List<ClientInfo> ListClients { get; set; } = new List<ClientInfo>();
同时将OnGet方法中的listClients.Add(clientInfo)改为ListClients.Add(clientInfo),前端循环时也对应改为Model.ListClients。
4. 补充HTML表格结构
原前端代码缺少表格标签,导致数据无法正常排版,补充完整的表格结构:
修正后的index.cshtml代码
@page @model Proiectss.Pages.IndexModel @{ } <br> <h2>List of Clients</h2> <a href="/Clients/Create">New Client</a> <table border="1"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Phone</th> <th>Address</th> <th>Created At</th> <th>Action</th> </tr> </thead> <tbody> @foreach (var item in Model.ListClients) { <tr> <td>@item.id</td> <td>@item.name</td> <td>@item.email</td> <td>@item.phone</td> <td>@item.adress</td> <td>@item.created_at</td> <td> <a href="/Clients/Edit?id=@item.id">Edit</a> | <a href="/Clients/Delete?id=@item.id">Delete</a> </td> </tr> } </tbody> </table>
修正后的index.cshtml.cs代码
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using System.Data.SqlClient; namespace Proiectss.Pages { public class IndexModel : PageModel { public List<ClientInfo> ListClients { get; set; } = new List<ClientInfo>(); private readonly ILogger<IndexModel> _logger; public IndexModel(ILogger<IndexModel> logger) { _logger = logger; } public void OnGet() { try { string connectionString = "Data Source=.\\sqlexpress;Initial Catalog=spital;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql = "SELECT * FROM clients"; using (SqlCommand command = new SqlCommand(sql, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while(reader.Read()) { ClientInfo clientInfo = new ClientInfo(); clientInfo.id = reader.GetInt32(0).ToString(); clientInfo.name = reader.GetString(1); clientInfo.email = reader.GetString(2); clientInfo.phone = reader.GetString(3); clientInfo.adress = reader.GetString(4); clientInfo.created_at = reader.GetDateTime(5).ToString(); ListClients.Add(clientInfo); } } } } } catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); } } } public class ClientInfo { public string id { get; set; } public string name { get; set; } public string email { get; set; } public string phone { get; set; } public string adress { get; set; } public string created_at { get; set; } } }
内容的提问来源于stack exchange,提问作者WisdomAnimates
相关产品推荐
相关产品推荐

