模型中的列表未反映在视图中

时间:2018-07-21 03:55:25

标签: c# asp.net-mvc model

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

Project.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; }
    }
}

Context.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; }
    }
}

DatabaseInitializer.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 ();
        }
    }
}

ProjectsController.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);
        }
    }
}

Details.cs

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

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

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

错误
Error message

粘贴在此处的每个代码块都已简化为相关代码。

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

1 个答案:

答案 0 :(得分:2)

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

例如:

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);
    }