Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 我应该为元素集合使用ViewModel:IList、IEnumerable还是列表类型?_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 我应该为元素集合使用ViewModel:IList、IEnumerable还是列表类型?

Asp.net mvc 我应该为元素集合使用ViewModel:IList、IEnumerable还是列表类型?,asp.net-mvc,Asp.net Mvc,我应该在ViewModel中使用元素集合:IList、IEnumerable还是列表类型 这是我的ViewModel: public class NoteWithCommentsViewModel { public Notes Note { get; set; } public IList<Comments> Comments { get; set; } //Maybe it should be: List or IEnumerable? } 在视图中,我迭代注释-

我应该在ViewModel中使用元素集合:IList、IEnumerable还是列表类型

这是我的ViewModel:

public class NoteWithCommentsViewModel
{
    public Notes Note { get; set; }
    public IList<Comments> Comments { get; set; } //Maybe it should be: List or IEnumerable?
}

在视图中,我迭代注释-因此IEnumerable在ViewModel中应该足够,但在controller中,我必须使用ToList执行查询,所以我可能应该在ViewModel中使用List或IList?

可能的重复项,那么我应该在ViewModel中使用IList或IEnumerable吗?为什么必须在controller中使用.ToList?您应该使用最抽象的类型这让你可以做你需要做的一切。
var note = _notesService.GetNote(friendlyUrl);
var comments = _commentsService.GetNoteCommentsWithoutSpam(note.NoteId).ToList();

var noteWithCommentsViewModel = new NoteWithCommentsViewModel
{
    Note = note,
    Comments = comments
};