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

ASP.NET MVC页面空白问题:ArtistAlbumIndex无数据且无法查看源码

启动网页后,页面跳转到地址:https://localhost:5001/ArtistAlbum/ArtistAlbumIndex,但页面无任何数据显示,且无法查看页面源码。该页面本应展示关联表对应的艺术家与专辑列表,编译运行后仅显示空白页,无任何报错信息可用于调试,求排查思路。


提供的代码片段

控制器代码

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using App.Models;
using ChinookContext;
using ChinookEntities;

namespace App.Controllers
{
    public class ArtistAlbumController : Controller
    {
        public IActionResult Index()
        {
            ArtistAlbumViewModel model = new ArtistAlbumViewModel();
            Chinook db = new Chinook();
            List<ArtistAlbum> artistAlbums = db.Artists.Join(db.Albums, 
                art => art.ArtistId, alb => alb.ArtistId, 
                (art, alb) => new ArtistAlbum() {
                    ArtistId = art.ArtistId,
                    Name = art.Name,
                    AlbumId = alb.AlbumId,
                    Title = alb.Title
                }
            ).ToList();
            return View(model);
        }
    }
}

视图模型类

using System;
using System.Collections.Generic;
using System.Linq;
using ChinookContext;
using ChinookEntities;

namespace App.Models
{
    public class ArtistAlbumViewModel
    {
        public List<ArtistAlbum> ArtistAlbums { get; set; }
    }
}

实体类

public class ArtistAlbum
{
    public Int32 ArtistId { get; set; }
    public String Name { get; set; }
    public Int32 AlbumId { get; set; }
    public String Title { get; set; }
}

ArtistAlbumIndex视图

@using App.Models
@using ChinookEntities;
@model ArtistAlbumViewModel

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AlbumAritst Index</title>
</head>

<body>
    <h1>This is the ArtistAlbum index page!</h1>
    @* <p>@Model.paragraph</p> *@
    <table class="table table-bordered table-responsive table-hover">
        <tr>
            <td style="text-align: center;">Working</td>
        </tr>
        @foreach (ArtistAlbum artistAlbum in @Model.ArtistAlbums)
        {
            <tr>
                <td>@artistAlbum.Name</td>
                <td>@artistAlbum.Title</td>
            </tr>
        }
    </table>
</body>

</html>

导航按钮代码

<li class="nav-item">
    <a class="nav-link text-dark" asp-area="" asp-controller="ArtistAlbum" asp-action="ArtistAlbumIndex">ArtistAlbum Index</a>
</li>

排查思路

1. 路由与控制器Action不匹配

导航按钮请求的Action是ArtistAlbumIndex,但控制器中仅定义了Index方法,ASP.NET无法找到对应处理逻辑,会返回空白页或404错误。

  • 修复方向:要么将控制器方法名改为ArtistAlbumIndex,要么修改导航按钮的asp-action属性为Index。

2. 视图模型未赋值

控制器中已查询出artistAlbums列表,但未将其赋值给model.ArtistAlbums属性,导致视图中@Model.ArtistAlbums为null。遍历null集合会触发空引用异常,导致页面渲染失败。

  • 修复代码:在控制器中添加model.ArtistAlbums = artistAlbums;,再执行return View(model);。

3. 视图文件与Action名称不匹配

控制器默认会查找与Action同名的视图文件(如Index.cshtml),但你的视图文件名为ArtistAlbumIndex.cshtml,如果Action是Index,会找不到正确视图,返回空白。

  • 修复方向:要么将视图文件名改为Index.cshtml,要么在控制器中指定视图名称:return View("ArtistAlbumIndex", model);。

4. 启用详细错误信息

空白页通常是因为异常被静默处理,可在Program.cs中启用开发环境详细错误页,直接查看具体异常:

app.UseDeveloperExceptionPage();

5. 验证数据库查询有效性

在控制器中添加断点,调试查看artistAlbums的数量和内容,确认db.Artists、db.Albums是否有数据,Join操作是否返回预期结果,排除数据库层面的问题。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 04:57:46