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

从特定漫画获取所属系列信息(ASP.NET MVC)【未解决】

Hey folks! I'm currently building a comic store website using the ASP.NET MVC framework, and I'm stuck on fetching data from my models properly. My goal is to look up a specific comic by its ID in the controller, while also pulling in the details of the series that comic belongs to. I've included my model code, ViewModel code (I reference this as "vm" in my view), and a screenshot to clarify the issue—any help would be really appreciated!

Problem Context

I need to retrieve a single comic and its associated series data, then pass both to my view using a ViewModel. Right now, either the series data isn't loading correctly (shows up as null in the view) or I'm running into issues with lazy loading/explicit loading.

My Models

Here's the code for my Comic and Series models:

public class Comic
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int IssueNumber { get; set; }
    public decimal Price { get; set; }
    public int SeriesId { get; set; }

    // Navigation property to the associated series
    public virtual Series Series { get; set; }
}

public class Series
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Publisher { get; set; }

    // Navigation property to all comics in the series
    public virtual ICollection<Comic> Comics { get; set; }
}

ViewModel (vm)

This is the ViewModel I'm using to pass data to the view:

public class ComicDetailsViewModel
{
    public Comic SelectedComic { get; set; }
    public Series AssociatedSeries { get; set; }
}

Controller Code

Here's what I've tried so far in my controller's action method:

public ActionResult Details(int id)
{
    // First attempt: Find comic, then fetch series separately
    var comic = db.Comics.Find(id);
    if (comic == null)
    {
        return HttpNotFound();
    }
    var series = db.Series.Find(comic.SeriesId);

    // Second attempt: Using Include to eager load the series
    // var comicWithSeries = db.Comics.Include(c => c.Series).FirstOrDefault(c => c.Id == id);

    var vm = new ComicDetailsViewModel
    {
        SelectedComic = comic,
        AssociatedSeries = series // Or comicWithSeries.Series if using the Include approach
    };

    return View(vm);
}

Issue Screenshot

When I try to display the series name in the view (using @vm.AssociatedSeries.Name), either it throws a null reference exception or the value is empty. Here's what the debug view shows:
Screenshot showing null AssociatedSeries in ViewModel

What I Need Help With

  • What's the correct way to eager load the associated Series data when fetching the Comic?
  • Am I structuring my ViewModel correctly for this use case?
  • Are there any best practices I'm missing for passing related model data to a view in ASP.NET MVC?

Thanks in advance for any advice or solutions you can share!

内容的提问来源于stack exchange,提问作者René Pedersen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:13:31