user547794
user547794

Reputation: 14511

Passing controller calculation logic to view with ViewBag

I have a controller that runs some calculation on data, and then I need to return the calculation to a view. I understand that I can accomplish this through the ViewBag, but I would like to know the best practice for doing this. Are these LINQ queries something that should just be executed in the View?

public ViewResult Results()
{
    var surveyresponsemodels = db.SurveyResponseModels.Include(
        s => s.SurveyProgramModel).Include(s => s.PersonModel);

    ViewBag.PatientFollowUpResult = db.SurveyResponseModels.Count(
        r => r.PatientFollowUp);

    ViewBag.ChangeCodingPractice = db.SurveyResponseModels.Count(
        r => r.ChangeCodingPractice);

    ViewBag.CountTotal = db.SurveyResponseModels.Count();

    return View(surveyresponsemodels.ToList());
 }

Upvotes: 0

Views: 570

Answers (1)

gdoron
gdoron

Reputation: 150253

The best practice is not using ViewBag at all.

Put all the data you want you want to use in the View in your ViewModel.

Keep the View clean for calculations, and use it only for presentations.

Upvotes: 4

Related Questions