2010-06-17 79 views
5

我一直在寻找mvc的视图模型,我正在寻找最好的方法来完成它们。我读过大量不同的文章,但没有一篇看起来像“最好的方式”那样清晰。到目前为止,例如,我可能有一个客户模型具有以下属性:asp.net mvc viewmodels。他们应该包含多少逻辑(如果有的话)

  • 标题
  • 位置

如果位置是一个外键的位置数据库中的表。

我希望能够编辑此客户,但只能输入名字,姓氏和位置。我对编辑中的标题不感兴趣。所以在我看来,我需要通过一个客户和一个选定的列表。

现在从我读过的书中我有以下选项(可能还有更多)。

所以我的问题基本上哪个是最好的?

1)

添加一个选择列表中ViewData["Location"]和刚刚创造客户的强类型的看法?

2)

创建其中I通过一个客户和选择列表的图模型(数据访问在控制器完成):

public class ViewModelTest 
{ 
    public Customer Customer { get; set; } 
    public SelectList Locations { get; set; } 

    public ViewModelTest(Customer customer, SelectList locations) 
    { 
     Customer = customer; 
     Locations = locations; 
    } 
} 

3)

创建视图模型我在那里传递客户和位置列表,并在视图模型中创建选择列表。

public class ViewModelTest 
{ 
    public Customer Customer { get; set; } 
    public SelectList Locations { get; set; } 

    public ViewModelTest(Customer customer, List<Location> locations, string selectedLocation) 
    { 
     Customer = customer; 
     Locations = new SelectList(locations, "LocationID", "LocationName", selectedLocation); 
    } 
} 

4)

传递一个客户和仓库,做在视图模型的数据访问。

public class ViewModelTest 
{ 
    public Customer Customer { get; set; } 
    public SelectList Locations { get; set; } 

    public ViewModelTest(Customer customer, IRepository repository, string selectedLocation) 
    { 
     Customer = customer; 
     Locations = new SelectList(repository.GetLocations(), "LocationID", "LocationName", selectedLocation); 
    } 
} 

5)

只需我需要的属性创建视图模型:

public class ViewModelTest 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public SelectList Locations { get; set; } 

    public ViewModelTest(Customer customer, SelectList locations) 
    { 
     FirstName = customer.FirstName; 
     LastName = customer.LastName ; 
     Locations = locations; 
    } 
} 

6)

还是上面的或其它方式的一些其它组合。

欢迎任何意见。

回答

6

这里是我建议:有反映强类型视图的领域视图模型:

public class SomeViewModel 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Location { get; set; } 
    public IEnumerable<SelectListItem> PossibleLocations { get; set; } 
} 

而在你的控制器动作填充这个视图模型:

public ActionResult Index() 
{ 
    var customer = Repository.GetCustomer(); 
    var locations = Repository.GetLocations(); 
    var viewModel = new SomeViewModel 
    { 
     FirstName = customer.FirstName, 
     LastName = customer.LastName, 
     Location = customer.Location, 
     PossibleLocations = new SelectList(locations, "LocationID", "LocationName", customer.Location); 
    }; 
    return View(viewModel); 
} 

[HttpPost] 
public ActionResult Index(SomeViewModel viewModel) 
{ 
    // TODO: Handle the form submission 
    return View(viewModel); 
} 

当然这样做模型和视图模型之间的映射手动显示我的例子可能会变得非常繁琐,在这种情况下,我会建议你看看AutoMapper

2

我有我的视图模型,因为这

public class SomeViewModel 
{ 
    public Customer Customer { get; set; } 
    public IEnumerable<Location> PossibleLocations { get; set; } 
} 

我的控制器是这样的:

public ActionResult Index() 
{  
    var viewModel = new SomeViewModel 
    { 
     Customer = Repository.GetCustomer(), 
     PossibleLocations = Repository.GetLocations() 
    }; 
    return View(viewModel); 
} 

然后你就可以在这样的观点在您的客户访问对象的一切:

Customer name - <%: Model.Customer.FirstName %> <%: Model.Customer.LastName %> 
Location - <%: Html.DropDownList("LocationID", new SelectList(Model.PossibleLocations as IEnumerable, "LocationID", "LocationName", Model.Location.LocationID))%> 
相关问题