ListViewModel中的Model或ViewModel

时间:2012-08-09 16:00:49

标签: asp.net-mvc model viewmodel

T应该是例如Customer还是CustomerViewModel?

绑定到Mvc名称空间的注释位于ListViewModel上,所以实际上我可以传递Customer对象。你觉得怎么样?

public class ListViewModel<T>
{
        [Required(ErrorMessage="No item selected.")]
        public int[] SelectedIds { get; set; }
        public IEnumerable<T> DisplayList { get; set; }        
}

更新

[HttpGet]
 public ActionResult Open()
 {
    IEnumerable<Testplan> testplans = _testplanDataProvider.GetTestplans();
    OpenTestplanListViewModel viewModel = new OpenTestplanListViewModel(testplans);         
    return PartialView(viewModel);
 }

public class OpenTestplanListViewModel
{
        public OpenTestplanListViewModel(IEnumerable<Testplan> testplans)
        {
           var testplanViewModels = testplans.Select(t => new TestplanViewModel
           {
             Name = string.Format("{0}-{1}-{2}-{3}", t.Release.Name, t.Template.Name, t.CreatedAt, t.CreatedBy),
              TestplanId = t.TestplanId,
           });

           DisplayList = testplanViewModels;
        }

        [Required(ErrorMessage = "No item selected.")]
        public int[] SelectedIds { get; set; }
        public string Name { get; set; }
        public IEnumerable<TestplanViewModel> DisplayList { get; private set; } 
}

public class TestplanViewModel
{
        public int TestplanId { get; set; }     
        public string Name { get; set; }           
}

 public class Testplan
 {
        public int TestplanId { get; set; }
        public int TemplateId { get; set; }
        public int ReleaseId { get; set; }
        public string CreatedBy { get; set; }       
        public DateTime CreatedAt { get; set; }       
        public Template Template { get; set; }
        public Release Release { get; set; } 
 }

1 个答案:

答案 0 :(得分:0)

理想情况下,

T应该是一个视图模型。具有引用域模型的视图模型是某种混合视图模型,而不是真实模型。但是如果您认为在这种特定情况下,域模型将与视图模型完全相同,那么您也可以保留它。