首页 > 解决方案 > 模型中的列表未反映在视图中

问题描述

我将首先向您展示我到目前为止所拥有的...

项目.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProtoSim.Models {
    public class Project {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Status { get; set; }
        public string Image { get; set; }
        public List<string> Images { get; set; }
        public List<string> ImageDescriptions { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime UpdatedDate { get; set; }
        public DateTime RemovedDate { get; set; }
        public DateTime RestoredDate { get; set; }
    }
}

上下文.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using ProtoSim.Models;

namespace ProtoSim {
    public class Context : DbContext {
        public Context () {
            Database.SetInitializer (new DatabaseInitializer ());
        }

        public DbSet<Project> Projects { get; set; }
    }
}

数据库初始化器.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using ProtoSim.Models;

namespace ProtoSim {
    internal class DatabaseInitializer : DropCreateDatabaseAlways<Context> {
        protected override void Seed (Context context) {
            context.Projects.Add (new Project () {
                Name = "ProtoSim Website",
                Description = "Website for ProtoSim business",
                Status = "In Progress",
                Image = "",
                CreatedDate = DateTime.Today,
                UpdatedDate = DateTime.Today,
                RemovedDate = DateTime.Today,
                RestoredDate = DateTime.Today
            });

            context.Projects.Add (new Project () {
                Name = "Bar Tap Prop",
                Description = "Network-enabled prop designed to interface with up to 25 taps",
                Status = "In Progress",
                Image = "#",
                Images = new List<string> { "#", "#", "#", "#", "#" },
                ImageDescriptions = new List<string> { "description one", "description two", "description three", "description four", "description five" },
                CreatedDate = DateTime.Today,
                UpdatedDate = DateTime.Today,
                RemovedDate = DateTime.Today,
                RestoredDate = DateTime.Today
            });

            context.SaveChanges ();
        }
    }
}

项目控制器.cs

using ProtoSim.Data;
using ProtoSim.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;

namespace ProtoSim.Controllers {
    public class ProjectsController : Controller {
        private Context _context = null;
        public ProjectsController () {
            _context = new Context ();
        }

        public ActionResult Details (int id) {
            Project project = _context.Projects
                .Where (i => i.Id == id)
                .SingleOrDefault ();

            if (project == null)
                return HttpNotFound ();

            return View (project);
        }
    }
}

详细信息.cs

@using ProtoSim.Models
@model ProtoSim.Models.Project

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Project Details";
}

<body>
    <p>@Model.Images[0]</p>
</body>

错误
错误信息

此处粘贴的每个代码块都已被剥离为相关代码。

我要做的就是访问Model.Images. 我不明白为什么我会遇到这个问题。希望我能得到一些帮助来弄清楚如何从详细信息视图中访问此列表。

标签: c#asp.net-mvcmodel

解决方案


实体框架不支持原始类型的集合。您可以创建一个实体(将保存到不同的表中)或进行一些字符串处理以将您的列表保存为字符串并在实体具体化后填充列表。

例如:

public class Project
{
    ...
    public List<Image> Images { get; set; }
}
public class Image
{
    public int Id { get; set; }
    public string FileName { get; set; }
    public string Url{ get; set; }
}
public class MyContext:DbContext
{
    public DbSet<Project> Projects { get; set; }
    public DbSet<Image> Images { get; set; }
}

并在您的详细信息操作中:

public ActionResult Details (int id) {
        Project project = _context.Projects.Include(i => i.Images)
            .Where (i => i.Id == id)
            .SingleOrDefault ();

        if (project == null)
            return HttpNotFound ();

        return View (project);
    }

推荐阅读