如何将数组从模型传递到视图?
我刚刚学习 ASP.NET MVC 3,最近我尝试了很多次传递数组/列表/ICollections 等,但不能。每次列表都是空的。
例如,当前项目:
模型:
public class Video
{
public int VideoID { get; set; }
public string Name { get; set; }
public ICollection<string> Tags { get; set; }
}
初始化程序 - 种子:
protected override void Seed(DatabaseContext context)
{
var videos = new List<Video>
{
new Video {
Name = "Video01",
Tags = new List<string> { "tag1", "tag2" },
};
videos.ForEach(s => context.Videos.Add(s));
context.SaveChanges();
base.Seed(context);
}
在视图中:我确实得到了名称
属性,但 Tags
完全是空的。
在调试中我得到Tags - Count: 0
。
这不是我第一次遇到这种情况,说实话,每次我试图通过这类事情时都会发生这种情况。有关该项目的一些信息:
ASP.NET MVC 3、Entity-Framework:Code First、SqlServerCe.4.0。
I'm just learning ASP.NET MVC 3, And recently I tried a lot of times to pass arrays/lists/ICollections etc. but couldn't. everytime the list was empty.
For example, the current project:
Model:
public class Video
{
public int VideoID { get; set; }
public string Name { get; set; }
public ICollection<string> Tags { get; set; }
}
Initializer - Seed:
protected override void Seed(DatabaseContext context)
{
var videos = new List<Video>
{
new Video {
Name = "Video01",
Tags = new List<string> { "tag1", "tag2" },
};
videos.ForEach(s => context.Videos.Add(s));
context.SaveChanges();
base.Seed(context);
}
In the view: I do get the Name
property, but the Tags
are completely empty.
In the debug I get Tags - Count: 0
.
This is not the first time it happens to me, to be honest it happens every single time when I try to pass those kind of stuff. a bit of info about the project:
ASP.NET MVC 3, Entity-Framework:Code First, SqlServerCe.4.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,实体框架不加载实体的关联,您需要显式指定它:
By default Entity Framework doesn't load associations of an entity, you need to specify it explicitly:
创建实体标签
或将标签存储到用逗号/分号或适合您的解决方案的任何内容分隔的字段
Crean an entity Tag
or store tags to one field separated with comma /semicolon or whatever fits for your solution