-1

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;
        }      
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
WPalombini
  • 691
  • 9
  • 25

3 Answers3

0

ALWAYS in the controller/viewmodel. NEVER the view. the view's only purpose is to just display data otherwise you are combining data retrieval/processing in your UI!. That's all. your logic should be done in the controller/business logic layer and your data should always be done in your DAL layer.

your viewmodel should always contain the data you are going to be displaying/binding to the view. Again, be it a singular item or a collection - just some form of data without strong references to EF, since EF is a data access/ORM mapping tier.

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
0

In my opinion (which is fine to disagree with btw); the 'rules' I impose on myself are the following:

  • The controller reads data from sources, such as files and database.
  • The viewmodel has all the data, it never reads from any other source besides it's own internal collections. However, it's fine if the viewmodel returns it's data sorted or otherwise queried in different ways. But again, only the in-memory collections and objects it received when it was constructed and returned by the controller.
  • The view only works with ready made lists and objects, it's only worry is being a view. That is, representing the data in some format, usually html.

This usually works pretty solid for me and has very few notable exceptions I've found.

My 2 ct.

Edit/Sidenote: I do use EF objects and lists of EF objects in my viewmodels. Some don't like this, but after many projects and never experiencing spaghetti fallout from doing that I honestly don't know what the fuss is about. It's just a model, it describes what your data looks like, so for pete's sake, use the EF poco.

Edit 2: In the particular example you give, I would encourage you to pass a custom viewmodel to the partial, which contains the 'bus' object as property. I've found it's incredibly likely (like around 90%) that you'll want to pass something additional to the partial in the future, like meta-data or something.

Gerben Rampaart
  • 9,853
  • 3
  • 26
  • 32
0

Always It would be good to go with view model assign the values to it and pass it to the partial. Data would be fetched from Data source and pass to the partial view. And view is intended only for the frontend , option 2 ( setting to view bag) would be suggestible only in very few rare cases where you have to store very light information to use multiple places.