0

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; } 
 }
4

1 回答 1

0

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

于 2012-08-09T16:04:17.317 回答