即使在MVC 5视图中传递,模型也为null

时间:2019-08-15 08:06:03

标签: c# asp.net-mvc

我正在尝试从数据库中获取多个URL路径图像

我试图在控制器中编写此代码

NourEntities3 db = new NourEntities3();           
    public List<Image> img;            

    public ActionResult Index()
    {
            var images = db.Images.ToList();
             img = images;
            return View(img);

    }
    public FileStreamResult GetImage(string filename)
    {
        string fn = Server.MapPath(filename);
        if (System.IO.File.Exists(fn))
        {
            var memoryStream = new MemoryStream(System.IO.File.ReadAllBytes(fn));
            return new FileStreamResult(memoryStream, MimeMapping.GetMimeMapping(System.IO.Path.GetFileName(fn)));
        }
        return null;
    }

我的视图看起来像这样:

@model IEnumerable<nouur.Models.Image>
@{ Layout = "~/Views/Shared/_Layout.cshtml";}
  @foreach (var item in Model){
<div class="portfolio-item set-bg fw19" data-setbg="@Url.Action("GetImage","Retrieve",new{ filename=item.ImagePath })">
<a href="@Url.Action("GetImage","Retrieve",new{ filename=item.ImagePath })" class="portfolio-view">loadmore</a>
</div>}

我的模特:

  public partial class Image  
{
    public int ID { get; set; }
    public string Title { get; set; }
    [DisplayName("Upload File")]
    public string ImagePath { get; set;} 
    public HttpPostedFileBase ImageFile { get; set; }
}

我期望从数据库中获取路径...但是似乎错误提示

  

模型为空

  

System.NullReferenceException:对象引用未设置为   对象的实例。

0 个答案:

没有答案