我试图在代码优先方法中争论,但我在理解上存在差距。我有一系列帖子和文件。
public class post {
public post(){attachements = new list<files>();}
[key]
public int id{get;set;}
public string title{get;set}
public string body{get;set;}
public virtual ICollection<Files> attachments {get;set;}
}
public class file{
[key]
public int id{get;set;}
public string filename {get;set;}
}
我认为我已经将 ViewModel 与以下内容一起使用,
public class MyView{
public post post {get;set;}
public list<files> files {get;set;}
public MyView(post p, list<file> f){this.post = p; this.files = f;}
列表和显示一切正常......但是当我尝试创建新帖子并归档时,一切都崩溃了。我尝试过使用 ViewModel,但我不确定如何将项目添加到视图模型中的列表中。
我根据我的“帖子”创建了一个视图,然后有一个部分视图,用于上传文件并将它们的列表保存在隐藏字段中,我可以让一切正常工作,我只是不确定这是否是一个优雅的解决方案。
如果我偏离基地,有人可以告诉我吗?我希望开始会议并希望收集一些对其他菜鸟有所帮助的回复。
I am trying to wrangle in the code first approach and I have a gap in understanding here. I have a collection of posts and files.
public class post {
public post(){attachements = new list<files>();}
[key]
public int id{get;set;}
public string title{get;set}
public string body{get;set;}
public virtual ICollection<Files> attachments {get;set;}
}
public class file{
[key]
public int id{get;set;}
public string filename {get;set;}
}
And I think I have the ViewModel down with the following
public class MyView{
public post post {get;set;}
public list<files> files {get;set;}
public MyView(post p, list<file> f){this.post = p; this.files = f;}
Everything works fine for List and Display ... but when I try to create a new post and files it all falls apart. I've tried using the ViewModel but I am not sure how to add items to the list in the view model.
I created a view based on my 'post' and then have a partial view that uploads files and keeps a list of them in a hidden field I can get everything to work, I am just not sure if that is the elegant solution.
Could someone let me know if I am way off base ... I hope to start the convo and hopefully collect some responses that will be helpful to other noobs.
发布评论
评论(1)
您需要使用 MVC 的列表模型绑定功能:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
有关如何协同工作的良好指南:
http://blog.stevensanderson。 com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
You'll need to use the List Model Binding features of MVC:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
For a good guide of how this can all work together:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/