In a view where we return a list of objects, and we need to demonstrate the quantity, such as a small report, using some conditions.
What is the most performative way to return data to view ?
Explaining better.
When we return an object list to view , for example:
Using the Microsoft example , we have:
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
And we return all Movies
registered in our controller , for example:
public ActionResult Index()
{
return View(db.Movies.ToList());
}
In this case, let's say we want to know the amount of movies and show in a table, we could bring in a ViewBag
like this:
public ActionResult Index()
{
var filmes = db.Movies.ToList();
ViewBag.TotoalFilmes = filmes.Count();
return View(filmes);
}
And in view only show ViewBag
, like this:
<p> Total: @ViewBag.TotalFilmes</p>
This is one way to do it, but another would be simply using Count()
directly in view , like this:
<p> Total: @Model.Count</p>
Here's my question: Which of the two forms is "better" (more performative)?
Remembering that in the example I just showed with a ViewBag
. But I wonder if we have a lot of data.
If you have a third way, which is better, I'll be happy to get to know you too.