c# - 模型中的列表未反映在视图中
问题描述
我将首先向您展示我到目前为止所拥有的...
项目.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
. 我不明白为什么我会遇到这个问题。希望我能得到一些帮助来弄清楚如何从详细信息视图中访问此列表。
解决方案
实体框架不支持原始类型的集合。您可以创建一个实体(将保存到不同的表中)或进行一些字符串处理以将您的列表保存为字符串并在实体具体化后填充列表。
例如:
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);
}
推荐阅读
- html - 尽管没有填充,但 Div 空间
- python - 如何使用 Python 将新列附加到 csv?
- javascript - 追加来自多个表单的数据以将所有记录存储在 json 中
- shell - Shell Scripting - 列出目录中文件的标题行
- xpath - 计算包含两组 1 且由 0 分隔的列表中 1 的数量
- sql - 从具有两个父级的子表中删除,而无需在 sql server 中进行级联删除
- http - 如何处理通过 HTTP 上传的大文件而不将请求存储在临时文件中
- ios - 在哪些情况下需要使用 MTLStoreActionStoreAndMultisampleResolve 而不是 MTLStoreActionMultisampleResolve?
- ruby-on-rails - 让用户在 Rails 应用程序中下载 CSV 文件
- c++ - 使用指针迭代一系列原始类型声明?