I would like to know your opinion about this: I have a linq query inside a partial view (the main page calls @html.action(mypartial) that returns data from a PartialViewResult) and I would like to see what is the best approach: Please justify your opinion.
1 - to have the linq query in the partial view: we know that in the controller we are only changing / manipulating the query (IENumerable). Database is contacted when rendering the View
2 - to have the linq query in the controller and pass ViewBags.
3 - create a view model, assign the values to it, then pass it to the partial.
This is the partial:
<div class="notification">
<div class="messages pull-left">
<div class="count">
@ViewBag.TotalMessages
</div>
</div>
<div class="notifications pull-left">
<div class="count">
@Model.jobs.SelectMany(x => x.jobMessages.Where(w => w.owner.Equals(false) && w.seenAt == null)).Count();
</div>
</div>
<div class="credits pull-left">
<span class="strong">@Model.transactionItems.Sum(x => x.quantity) </span> credits
</div>
<div class="creditPurchase pull-left">
<a href="#">Buy credits</a>
</div>
</div>
In the controller, I have this:
public PartialViewResult BusinessNotificationPartial()
{
if (MySession.Current.Account.accountType == "business")
{
int businessId = MySession.Current.Businesses.FirstOrDefault().businessId;
UnitOfWork uow = new UnitOfWork();
BusinessRepository busRepo = new BusinessRepository(uow);
var bus = busRepo.GetById(businessId);
return PartialView("_BusinessNotificationPartial", bus);
}
else
{
return null;
}
}