What’s better?
1) If i make 3 ViewBag on server and then Render my View using this ViewBags?
Server
ViewBag.LeftColumnTales = tales.Where((x, i) => i % 3 == 0);
ViewBag.CenterColumnTales = tales.Where((x, i) => i % 3 == 1);
ViewBag.RightColumnTales = tales.Where((x, i) => i % 3 == 2);
View
<div id="left_column">
@foreach (var t in ViewBag.LeftColumnTales)
{
<div class="item">
<a href="/narodnie-skazki/@t.PeopleTalesCategory.RouteNameAn/@t.RouteNameAn">@t.NameAn</a> <span>(@(new HtmlString(Html.TimeForReadingHtmlResult((int)t.TimeForReading))))</span>
@(new HtmlString(Html.PeopleTaleVoterHtmlResult((int)t.Analit)))
</div>
}
<!--end of div.item-->
</div>
or
2) If i set ViewBag.tales on server and then on View will make Converting from dynamic data to IEnumerable and devide it to 3 columns?
Server
ViewBag.Tales = tales;
View
<div id="left_column">
@foreach (var t in ((IEnumerable<MVCFairyTales3.Models.AuthorTale>)ViewBag.Tales).Where((x, i) => i % 3 == 0))
{
<div class="item">
<a href="/avtorskie-skazki/@t.AuthorTalesCategory.RouteNameAn/@t.RouteNameAn">@t.NameAn</a> <span>(@(new HtmlString(Html.TimeForReadingHtmlResult((int)t.TimeForReading))))</span>
@(new HtmlString(Html.AuthorTaleVoterHtmlResult((int)t.Analit)))
</div>
}
<!--end of div.item-->
</div>
To be honest I don’t like any of those two. They both use ViewBag and weak typing. I am sorry but personally I get sick when I see ViewBag/ViewData.
Personally I like using view models and strongly typed views:
which you could populate in your action:
and in the strongly typed view:
and in the corresponding display template which will be rendered for each element of the collection (
~/Views/Shared/DisplayTemplates/Tale.cshtml):And even better if you have to repeat this all over your pages is to put it in the _Layout using child actions as explained by Phil Haack in his blog post.