3

I'm a bit new to ASP.Net MVC, but am trying to understand when and how best to use ViewModels.

In many of my Views, the data required is only partially what resides in one Model. For that, I'm using AutoMapper to map the Model to the ViewModel and then passing the ViewModel to the View.

However, in other views, data from multiple Models are required.

The way I have been accomplishing this so far is like so:

public class GamesEditData
{
    public Game Game { get; set; }
    public ICollection<Genre> Genres { get; set; }
}

...where Game and Genre are Models. I feel like I'm doing something wrong here, though, since this is directly passing the Models themselves to the View. This is different than the other times where I am passing properties from a separate ViewModel, which makes me think I'm breaking some kind of pattern.

In short, what is the best way to populate the View with multiple Models using a ViewModel design pattern in ASP.Net MVC?

4

1 回答 1

6

是的,我就是这样做的。如果您需要在模型中添加更多元素或使用属性装饰模型,以后扩展它很简单。

或者,您可以使用 a Tuple,将您的模型类型声明为:

@model Tuple<Game, ICollection<Genre>>
于 2013-06-14T01:26:52.813 回答