2

I currently have an application with a home page that shows a list of ten movies based on the date they were "created", or entered into the database. I would also like to show a list of the top ten movies based on the rating of each movie. Is there a way to pass in another model or alter my current ViewModel to do this? Here is the Index section of my Home Controller:

    public ActionResult Index()
    {
        var model =
            _db.Movies
                .OrderByDescending(m => m.DateEntered)
                .Take(10)
                .Select(m => new MovieListViewModel 
                {
                    Id = m.Id,
                    Title = m.Title,
                    Genre = m.Genre,
                    ReleaseDate =  m.ReleaseDate,
                    CountOfReviews = m.Reviews.Count()
                });

        return View(model);

    }

And the ViewModel being passed in:

public class MovieListViewModel
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Genre { get; set; }

    [Display(Name="Year Released")]
    public DateTime ReleaseDate { get; set; }
    public int CountOfReviews { get; set; }
}
4

5 回答 5

7

Create a model that encompasses both lists:

public class MovieListViewModel
{
  public List<MovieModel> Top10ByCreated { get; set; }
  public List<MovieModel> Top10ByRating { get; set; }
}

public class MovieModel
{
  public int Id { get; set; }
  public string Title { get; set; }
  public string Genre { get; set; }

  [Display(Name="Year Released")]
  public DateTime ReleaseDate { get; set; }
  public int CountOfReviews { get; set; }
}

Then in your controller:

var model = new MovieListViewModel();

model.Top10ByCreated = ...
model.Top10ByRating = ...

return View(model);

In your view, use MovieListViewModel as your model and use your two lists as needed.

于 2013-05-24T19:05:02.900 回答
2

只需创建另一个视图模型,两个列表具有两个属性:

public class MovieIndexViewModel
{
    public IEnumerable<MovieListViewModel> TopTenByDate { get; set; }
    public IEnumerable<MovieListViewModel> TopTenByRating { get; set; }
}

在控制器中,您可以创建此视图模型并传递两个列表:

public ActionResult Index()
    {
        var vm = new MovieIndexViewModel();

        vm.TopTenByDate = ....;
        vm.TomTenByRating = ...;

        return View(vm );

    }
于 2013-05-24T19:06:44.480 回答
1

You can compose a single view model that contains the two result sets. The controller then instantiates the parent type and populates the child collections as you see fit.

public sealed class HomeViewModel{
    public MovieListViewModel TopTenByRating { get; set; }
    public MovieListViewModel TopTenByDate { get; set; }
}

public ActionResult Index()
{
    var model = new HomeViewModel();

    model.TopTenByDate =
        _db.Movies
            .OrderByDescending(m => m.DateEntered)
            .Take(10)
            .Select(m => new MovieListViewModel 
            {
                Id = m.Id,
                Title = m.Title,
                Genre = m.Genre,
                ReleaseDate =  m.ReleaseDate,
                CountOfReviews = m.Reviews.Count()
            });

    model.TopTenByRating = // something else

    return View(model);
}

For more complex scenarios, I prefer a dedicated composer that is responsible for setting up the view model (rather than having all the logic in the action method).

For a simple scenario like this, setting it up in the controller is the easiest way. If you find yourself needing to reuse the query logic, consider abstracting it to a helper class.

于 2013-05-24T19:05:22.843 回答
0

一种不同的方法是创建主视图并让它呈现 2 个操作方法的结果,返回 2 个部分视图,每个部分视图键入一个模型。

于 2013-05-24T19:08:41.813 回答
0

您有不同的选择,您可以使用其中的任何一个

使用视图模型

对于视图模型,您必须创建一个类,在此类中,您将所有模型定义为此类的属性。这里有两个类。

public class EmployeeDetails
{
    [Required]
    [Display(Name = "Name")]
    public string Name { get; set; }

}

public class Employee
{
    public int Id { get; set; }
}

这是视图模型

public class ViewModel
{
    public Employee emp { get; set; }
    public EmployeeDetails empdet{ get; set; }
}

现在在控制器中你会这样做

public ActionResult About()
{
        ViewModel vm = new ViewModel();
        vm.emp = new Employee();
        vm.empdet = new EmployeeDetails();
        return View(vm);
}

鉴于您将收到这样的

@model ViewModel

使用元组

元组用于存储不同的类型。您可以将所需的类对象存储在其中并传递给视图

在控制器中

Tuple<int, string> tuple = new Tuple<int, string>(1, "Hello world");
return View(tuple);

鉴于你会收到这样的

@model Tuple<int,string>
于 2014-12-29T09:08:19.067 回答