asp.net - 为什么我无法将图像填充到 ASP.NET MVC 应用程序中的表中?
问题描述
我正在尝试在我的桌子上显示每辆车的图像。当我运行代码时,视图崩溃并出现错误
你调用的对象是空的
我的数据库中保存了图像,并且它们链接到汽车,所以我不确定为什么我得到空值。
这是我的看法
@model IEnumerable<IgnitionHub2._0.Models.Car>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ImageList.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.CarID)
</th>
<th>
@Html.DisplayNameFor(model => model.MarketValue)
</th>
<th>
@Html.DisplayNameFor(model => model.Year)
</th>
<th>
@Html.DisplayNameFor(model => model.Model.Make.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<img src=@String.Format("/CarImages/{0}",item.Images.FirstOrDefault().Title) />
</td>
<td>
@Html.DisplayFor(modelItem => item.CarID)
</td>
<td>
@Html.DisplayFor(modelItem => item.MarketValue)
</td>
<td>
@Html.DisplayFor(modelItem => item.Year)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model.Make.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.CarID }) |
@Html.ActionLink("Details", "Details", new { id = item.CarID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CarID })
</td>
</tr>
}
</table>
这是控制器
public ActionResult _Index()
{
var cars = new List<Car>(db.Cars);
return PartialView(cars);
}
这些是模型:
namespace IgnitionHub2._0.Models
{
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
public partial class Car
{
public Car()
{
}
public int CarID { get; set; }
public string Year { get; set; }
public string Color { get; set; }
public string Mileage { get; set; }
public string BodyType { get; set; }
public string Drive { get; set; }
public string Notes { get; set; }
public bool Available { get; set; }
public string VinNumber { get; set; }
public int CarLotID { get; set; }
public int ModelID { get; set; }
public virtual Model Model { get; set; }
public virtual ICollection<Image> Images { get; set; }
public HttpPostedFileBase[] files { get; set; }
public IEnumerable<Car> SelectedCar { get; set; }
public virtual Image ImageList { get; set; }
}
}
这是图像的模型
namespace IgnitionHub2._0.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
public partial class Image
{
public int ImageID { get; set; }
public int CarID { get; set; }
public string Title { get; set; }
public string ImagePath { get; set; }
public virtual Car Car { get; set; }
public HttpPostedFileBase[] files { get; set; }
}
}
当它到达@foreach (var item in Model)
视图中时,我的应用程序崩溃了。
请帮忙!先感谢您。
解决方案
我能够通过更改使我的代码工作
<td>
<img src=@String.Format("/CarImages/{0}",item.Images.FirstOrDefault().Title) />
</td>
到:
<td>
@if (item.Images.Count > 0)
{<img src=@String.Format("/CarImages/{0}", item.Images.FirstOrDefault().Title) />}
</td>
推荐阅读
- python - 让 Flask 在我的 Firestore 树中监听实时更新
- javascript - 需要在 Javascript 或 Angular 中搜索时显示来自 JSON 的父子节点
- angular - 'mat-toolbar' 不是已知元素:
- ssl - 如何使用 SSL 证书配置 Jasmin 短信网关?
- java - 预期为 BEGIN_OBJECT,但经过改造后为 BEGIN_ARRAY
- c# - c# ManagedInstallerClass.InstallHelper 块服务可执行文件
- r - R中数据框列的动态变异,无法弄清楚如何使用函数或循环
- python - 如何在 Python 中使用 VPN 进行网页抓取?
- javascript - Datepicker 项目未刷新
- c - 指向作为函数参数的指针的指针