1

I have model and viewBag in the same action method. in my case i can get the model data's in the view but ViewBag is not showing in the view

public IActionResult Projects()
    {
            var info = db.test
            .Where(x => x.Id == y.Id)
            .OrderByDescending(x => x.Project)
            .ToList();

            var count = db.test
            .Where(x => x.Id == test.Id)
            .Select(x => x.amount)
            .Sum();
       ViewBag.count = count ;

        return View(info);
    }

view:-

 @model IEnumerable<website.Models.Project>
    @{
        ViewData["Title"] = "Project";
     }
   
<div class="card-header p-1">                                
            <h4 class="card-title ">Project
            <span class="blue-grey lighten-2 font-small-3">@ViewBag.count</span>
            </h4>                                                
            <span class="badge">Approved</span>                                                        </div>
  • Does this answer your question? [ViewBag, ViewData and TempData](https://stackoverflow.com/questions/7993263/viewbag-viewdata-and-tempdata) – Ali Bigdeli Aug 04 '20 at 11:00
  • @MohamedAbubakkar I think (don't quote me) but Sum might return null if there are no rows - I would add in a test and see what happens. – Richard Housham Aug 05 '20 at 10:52

2 Answers2

0

Use tuple to get both the values as like Tuple<model, int>

0

You could take the raw count and stick it in a label:

@Html.Label((string)ViewBag.Count())
Pat Hermens
  • 906
  • 6
  • 19