2012-08-09 70 views
0

应该是一个例如Customer或CustomerViewModel?ListViewModel中的模型或ViewModel

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

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

UPDATE

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

回答

0

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

+0

我已经用完成的代码示例更新了我的问题。我正在将我的数据库中的测试计划列表传递给viewmodel的ctor。我的Testplan业务对象与我的TestplanViewModel看起来很不一样。有什么需要改进的吗?顺便说一句。我删除了通用视图模型。 – Elisabeth 2012-08-09 18:07:44

+0

您的OpenTestplanListViewModel视图模型没有默认的构造函数。这是有问题的,因为如果您决定将此视图模型用作动作参数,它将无法工作,因为默认的模型联编程序不会知道如何实例化它。另外你的'DisplayList'属性的setter是私有的,默认的模型联编程序将不能调用它。当然,如果你不打算使用这个视图模型作为动作参数,而只是将它传递给视图,那么这样的自定义构造函数没有问题。 – 2012-08-10 06:16:50

+0

哈哈很好的答案。现在你说过了,我再次记住......我发现了另一个带有2个构造函数的ViewModel,我不记得我为什么这么做了......我纠正了你的2个评论者,因为这个vm是作为动作参数生成的。谢谢Darin。 – Elisabeth 2012-08-10 07:26:58