You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core .NET6 Razor Pages单cshtml页面如何展示多表/多模型数据

解决方法

你遇到的报错核心原因是:Razor Pages的视图只能访问对应PageModel(你代码里的CreateModel)中公开定义的属性,无法直接读取数据库表数据,需要你手动在PageModel中新增需要用到的多模型属性、查询数据后传递给视图。

步骤1:修改后台CreateModel.cs文件

在你的TestProject.Pages.Class.CreateModel类中添加User_Profile相关属性,并且在页面加载时查询对应数据:

using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using TestProject.Models; // 替换为你自己的模型所在命名空间

namespace TestProject.Pages.Class
{
    public class CreateModel : PageModel
    {
        private readonly YourDbContext _context; // 替换为你项目的DbContext类名

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

        // 原有用于提交Class数据的绑定属性,保留即可
        [BindProperty]
        public Models.Class Class { get; set; }

        // 新增用于展示的User_Profile集合属性
        public List<User_Profile> User_Profiles { get; set; }

        public async Task OnGetAsync()
        {
            // 从数据库查询所有User_Profile数据赋值给属性
            User_Profiles = await _context.User_Profile.ToListAsync();
        }

        // 原有Post提交方法调整,提交失败时需要重新赋值User_Profiles避免页面表格无数据
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                User_Profiles = await _context.User_Profile.ToListAsync();
                return Page();
            }

            _context.Class.Add(Class);
            await _context.SaveChangesAsync();
            return RedirectToPage("./Index");
        }
    }
}

步骤2:修正前端cshtml代码

修正属性名大小写不一致、无空值判断的问题,避免空引用异常:

<!-- 新增空值判断,没有数据时不会渲染表格也不会报错 -->
@if (Model.User_Profiles != null && Model.User_Profiles.Any())
{
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.User_Profiles[0].FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.User_Profiles[0].LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.User_Profiles[0].ProfileType)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.User_Profiles[0].Status)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
    @foreach (var item in Model.User_Profiles) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td> 
                @Html.DisplayFor(modelItem => item.ProfileType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Status)
            </td>
        </tr>
    }
    </tbody>
</table>
}

补充说明

Razor Pages没有单页面只能绑定一个模型的限制,所有需要在视图中访问的多表数据,都可以通过在PageModel中定义公开属性、提前查询赋值的方式传递到视图。如果需要在提交表单时同时传递User_Profile的选中值,只需给对应的属性加上[BindProperty]特性即可。

内容的提问来源于stack exchange,提问作者Reet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 03:15:04